13

So I have an Activity A and an Activity B. I want Activity A to be able to navigate to Activity B with the press of a button. That works, but when I use the up navigation(the home button in the action bar) to navigate back to Activity A, onCreate() is called again and the old information that the user typed in is lost.

I've seen: onCreate always called if navigating back with intent, but they used Fragments, and I'm hoping not to have to redesign the entire app to use fragments. Is there any way I can stop onCreate() from being called every time Activity A becomes active again?

Community
  • 1
  • 1
Abe Fehr
  • 729
  • 9
  • 23
  • Up navigation should be able to return from ActivityB to ActivityA without calling `onCreate()`. You must be doing something funky. Please post the code you are using to do this. Also, are you sure that ActivityA hasn't been already finished when ActivityB is running? – David Wasser Oct 09 '13 at 17:30

3 Answers3

16

This behavior is totally fine and wanted. The system might decide to stop Activities which are in background to free some memory. The same thing happens, when e.g. rotating the device.

Normally you save your instance state (like entered text and stuff) to a bundle and fetch these values from the bundle when the Activity is recreated.

Here is some standard code I use:

private EditText mSomeUserInput;
private int mSomeExampleField;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO inflate layout and stuff
    mSomeUserInput = (EditText) findViewById(R.id.some_view_id);

    if (savedInstanceState == null) {
        // TODO instanciate default values
        mSomeExampleField = 42;
    } else {
        // TODO read instance state from savedInstanceState
        // and set values to views and private fields
        mSomeUserInput.setText(savedInstanceState.getString("mSomeUserInput"));
        mSomeExampleField = savedInstanceState.getInt("mSomeExampleField");
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // TODO save your instance to outState
    outState.putString("mSomeUserInput", mSomeUserInput.getText().toString());
    outState.putInt("mSomeExampleField", mSomeExampleField);
}
flx
  • 14,146
  • 11
  • 55
  • 70
  • 1
    That's a really good solution! I had no idea that's what the bundles were for, but I'll find a way to use them to store the user's information. Thanks so much! – Abe Fehr Oct 07 '13 at 03:45
  • Very curious. 3 years developing for Android I haven't ended up with this simple Android behaviour. It's because playing with intents sometimes can be tricky :-) – cesards Feb 09 '14 at 19:11
  • 2
    Is there a way to skip executing onCreate on returning to Activity A from B using UP Navigation? I actually make network calls to fetch data, and executing it again takes more time. – Samarth Agarwal Feb 10 '15 at 05:57
  • If it's an application where the user wants to go back and forth frequently, you wouldn't want to do that since your app'll need to bind the controls again and again, which takes time, just like @SamarthAgarwal pointed for his network app. Samarth, could you find a solution for it? – Akash Agarwal Apr 02 '16 at 00:27
  • 1
    ohh wow thats what that Bundle in the onCreate is actually used for...had no idea!! – Manny265 Aug 23 '16 at 21:50
15

You can make the up button behave like pressing back, by overriding onSupportNavigateUp()

 @Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}
Silvia H
  • 8,097
  • 7
  • 30
  • 33
  • 2
    There's a reason why android has up and back button. Quote from: http://developer.android.com/training/implementing-navigation/temporal.html "Back navigation is how users move backward through the history of screens they previously visited. All Android devices provide a Back button for this type of navigation, **so your app should not add a Back button to the UI.**" – Akash Agarwal Apr 02 '16 at 00:22
7

If you want to navigate from child to parent without recreating the parent (calling onCreate method), you may set the android:launchMode="singleTop" attribute for the parent activity in your AndroidManifest.xml

baris1892
  • 981
  • 1
  • 16
  • 29
  • 1
    This works, and is the simplest solution to this problem. If someone is looking for an explanation: setting `launchMode` to `singleTop` ensures only one instance of an activity is present in the stack at a time. So on navigating using the 'up' button from an activity, instead of creating a new instance of the parent activity, it just brings the existing instance to top of the stack. – Aswin G Oct 19 '18 at 20:05