0

I did a back button by writing in the manifest file(like there http://developer.android.com/training/implementing-navigation/ancestral.html)

<activity android:name=".Activity2"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"
            />

    </activity>

How do I transfer data from Activity 2 when returning to MainActivity?Or there is a better way to make back button in action bar?

Sergey
  • 421
  • 1
  • 5
  • 14
  • Shared Preferences? Singleton? Lots of ways. – Kristy Welsh May 02 '16 at 15:04
  • Well, you could also launch the parent activity with an intent and put the data in extras. Check the available flags so the activity starts in the correct mode. – miva2 May 02 '16 at 15:13

3 Answers3

1

First, invoke Activity2 from MainActivity using Activity.startActivityForResult().

In Activity2, once the user has done the action that will set up the data you want, call Activity.setResult(). You can set a resultCode of RESULT_OK and pass an Intent here containing the Parcelable data you want to return.

Finally, you will override Activity.onActivityResult() in your MainActivity. This will be called when Activity2 finishes. Here you you will receive the resultCode of RESULT_OK and you can access the Intent that was set up by your Activity2.

See this link Starting Activities and Getting Results | Activity | Android Developers

kris larson
  • 30,387
  • 5
  • 62
  • 74
0

The proper way would be to use startActivityForResult() to start the SecondActivity and use onActivityResult() to receive the data from SecondActivity to the FirstActivity.

Refer here: http://developer.android.com/training/basics/intents/result.html

Bob
  • 13,447
  • 7
  • 35
  • 45
0

From MainActivity you can start Activity2 using startActivityForResult() - then in the second Activity you can set the result that you want to be sent back to the MainActivity when the user clicks the Back Button. You do something like:

Intent resultIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,resultIntent );

Alternatively, you could override getParentActivityIntent () (or getSupportParentActivityIntent() if using Support Library) in Activity2 like:

@Override
public Intent getParentActivityIntent () {
    Intent intent = super.getParentActivityIntent();
    //here you put the data that you want to send back - could be Serializable/Parcelable, etc
    intent.putExtra("result",result);
    return intent;
}

You can check out the selected answer here, and explore some options here

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • How can i track when user click the back button? – Sergey May 02 '16 at 15:46
  • You can override the `onBackPressed` method like this `@Override public void onBackPressed() { // user clicked back button? Do something here (but do not change the expected behavior as that might confuse the user... }` – ishmaelMakitla May 02 '16 at 16:03