Sending data back to the Main Activity in Android

android go back to previous activity with data
android return result from activity
android startactivityforresult return result
android pass data to activity
startactivityforresult android
android go back to previous activity without finish
setresult android
get data from another activity android

I have two activities: main activity and child activity. When I press a button in the main activity, the child activity is launched.

Now I want to send some data back to the main screen. I used the Bundle class, but it is not working. It throws some runtime exceptions.

Is there any solution for this?

There are a couple of ways to achieve what you want, depending on the circumstances.

The most common scenario (which is what yours sounds like) is when a child Activity is used to get user input - such as choosing a contact from a list or entering data in a dialog box. In this case you should use startActivityForResult to launch your child Activity.

This provides a pipeline for sending data back to the main Activity using setResult. The setResult method takes an int result value and an Intent that is passed back to the calling Activity.

Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
resultIntent.putExtra("some_key", "String data"); 
setResult(Activity.RESULT_OK, resultIntent);

To access the returned data in the calling Activity override onActivityResult. The requestCode corresponds to the integer passed in in the startActivityForResult call, while the resultCode and data Intent are returned from the child Activity.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch(requestCode) {
    case (MY_CHILD_ACTIVITY) : {
      if (resultCode == Activity.RESULT_OK) {
        // TODO Extract the data returned from the child Activity.
        String returnValue = data.getStringExtra("some_key");

How to send data back to the main Activity in android?, This example demonstrates how do I send data back to the main activity in android.Step 1 − Create a new project in Android Studio, go to File  getStringExtra() method is for get the data(key) which is send by above method. according the data type of value there are other methods like getIntExtra(), getFloatExtra() How to create an Android App to send and receive the data between two activity. Step 1: Firstly create a new Android Application. This will create an XML file and a Java File.

Activity 1 uses startActivityForResult:

startActivityForResult(ActivityTwo, ActivityTwoRequestCode);

Activity 2 is launched and you can perform the operation, to close the Activity do this:

Intent output = new Intent();
output.putExtra(ActivityOne.Number1Code, num1);
output.putExtra(ActivityOne.Number2Code, num2);
setResult(RESULT_OK, output);

Activity 1 - returning from the previous activity will call onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ActivityTwoRequestCode && resultCode == RESULT_OK && data != null) {
        num1 = data.getIntExtra(Number1Code);
        num2 = data.getIntExtra(Number2Code);

UPDATE: Answer to Seenu69's comment, In activity two,

int result = Integer.parse(EditText1.getText().toString()) 
           + Integer.parse(EditText2.getText().toString());
output.putExtra(ActivityOne.KEY_RESULT, result);

Then in activity one,

int result = data.getExtra(KEY_RESULT);

Sending data back to the Main Activity In Android, When we open another activity, we can send data to it by using an Send Data Back from Duration: 16:34 Posted: Jan 16, 2018 Pass information back from one activity to the Activity which launched it. Covers: - launching the 2nd activity - setting up a value to return (via an Intent) - receiving the value in the 1st

Send Data Back from Child Activity with startActivityForResult , Sending data back to the Main Activity In Android How to pass data from 2nd activity to 1st Duration: 19:00 Posted: Mar 2, 2017 Passing Data Between Activities Android Tutorial Jerry Zhao December 9, 2017 12 Transfer data between different activity is a common use case in android application.

FirstActivity uses startActivityForResult:

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, int resultCode); // suppose resultCode == 2

protected void onActivityResult(int requestCode, int resultCode, Intent data)
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 2)
        String message=data.getStringExtra("MESSAGE");

On SecondActivity call setResult() onClick events or onBackPressed()

Intent intent=new Intent();

Lesson 14 Android : Sending data back to the Main Activity In , Let's say you have 2 activities and you want to send back an information such as a value from a form. Here is the situation I was facing: The app has a flow that  (You'll often see back Buttons used to emulate the behavior of an iOS UINavigationController navigationBar which is discouraged in Android apps). If your main activity launches a stack of Activities and you want to provide an easy way to get back to the main activity without repeatedly pressing the back button, then you want to call

Call the child activity Intent using the startActivityForResult() method call

There is an example of this here:

and in the "Returning a Result from a Screen" of this:

Android, You can also start another activity and receive a result back. The APIs provide default contracts for basic intent actions like taking a picture, requesting  I don't know if this is the best way or not Bu I have been searching on google for quite a while finding how can I pass a Bundle from a fragment to its container activity but all I found was sending data from activity to fragment instead (which was a bit confusing for me as I'm a newbie).

Getting a result from an activity, Let's assume you need to pass data from MainActivity to an If you need to start an Activity which can then return back a result then when  Android is one of the most popular operating systems for mobiles. I am sure all the chat applications use this function to transfer the message user activity to another user activity. I will show you how to send the data one activity to another activity in an android application using the Android studio. Android is the kernel-based operating

Pass Data Between Android Activities, For example, if you start SecondActivity from MainActivity, you can get data back from SecondActivity when it closes. +. Returning data from an Activity only 

Sending Data Between Activities · Android Sea, How send data from second activity to first activity in Android?

  • Possible duplicate of How do I pass data between Activities in Android application?
  • One more trick define an ArrayList in your main activity and make it static so you can access it in second activity then add data in itthat you want to send to main activity then you access it in main activity
  • for completeness one should mention what is the best place to place the call to finish()? It maybe clear to experts, but for the beginners would be good to know without referencing additional sources.
  • @jelmoodjasser It took me a bit to figure out, but basically when you start the new activity with Intent you need to use the startActivityForResult function instead of just startActivity. An example might be startActivityForResult(myIntent, 2); where 2 is the result code, which can take the place of MY_CHILD_ACTIVITY in the switch statement above.
  • when second activity finished and returns to first activity then how to set requestCode into second activity before finish it.... for use it to onActivityResult in FirstActivity
  • Hi there I appreciate u for answering my question.This code is not enough for me. I want addition to be performed in the second activity itself and the result should be returned to the MainActivity through onActivityResult method. For instance there is only button in the Main Activity which takes u to second activity on clicking it, there two numbers are entered through the editText widget, addition logic is performed in the second activity itselfand finally the result is returned to MainActivity. Got it?
  • In that case in the second activity, you would perform the calculation and store the result in the intent with putExtra(). I've edited my answer above
  • That is a very well written an explicit explanation. Well done!
  • Yes, I concur with cbrulak, the link to the docs was way more helpful than the answer.
  • The links are showing some general things now. The content might be changed please update it or remove the answer for community
  • well explained!
  • This would be more appropriate if the second activity was setting a permanent change/setting in the application.
  • Will this work if I want to share data between 2 different android apps?One referred as library ?
  • This is SharedPreferences abuse.
  • Using this method for just passing data between two activities (OP's original question) is like abusing SharedPreferences. It is not meant for this and the system has to do too much work (writing xml on storage and reading it again) for a simple task like passing data between two activities.