31

I'm using the Actionbar and it's "up" button to return from a detail activity to the main activity, which works fine. Similarly, the user can press the system "back" button to return to the main activity.

In my main activity, in onCreate() data is downloaded from the internet to display upon app start. I noticed that when I use the Actionbar "up" button to go from detail to main activity, onCreate() is run, re-downloading the data. But onCreate() is not run when I use the system "back" button, therefore immediately showing the main activity view.

The code I use in the detail activity to implement the "up" button is:

switch (item.getItemId()) {
   case android.R.id.home:
      Intent intent = new Intent(this, MainActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(intent);
      return true;

I would like the "up" button to behave like the "back" button and not rerun onCreate(). But I'm unsure how to make this happen, or which code path the "back" button implements to return to the main activity.

Thanks!

mraviator
  • 4,034
  • 9
  • 38
  • 51
  • possible duplicate of [Proper way to handle action bar up button?](http://stackoverflow.com/questions/11304483/proper-way-to-handle-action-bar-up-button) – the911s May 03 '14 at 02:34

6 Answers6

49

Instead of starting a whole new activity simply finish the details activity you are in

switch (item.getItemId()) {
   case android.R.id.home:
      finish();
      return true;

Then you will return to the previous activity on the activity stack (your main activity) and onCreate shouldn't be called

dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • DOH! Now why didn't I think of that? I guess I'm trying to stick too close to the documentation.... Thanks! – mraviator Apr 25 '12 at 17:11
  • 13
    This is **not** always the right solution. sometimes the activity start from another application and `up` should start the parent activity. more about it here: http://developer.android.com/training/implementing-navigation/ancestral.html – shem Mar 18 '13 at 13:55
  • 4
    Yes, but in google solution, `onCreate` called even when the activity in stack, do you know how to use mimic 'back' functionality when activity already exists, and create one otherwise? – shem Mar 18 '13 at 14:10
  • `android.R.id.home` **home** is added in *api 11*! whats for 2.2 and above? – Muhammad Babar Aug 18 '14 at 11:35
  • 4
    @shem know its late(may help some one, some day) use `android:launchMode="singleTop"` on your parentActivity! – Muhammad Babar Aug 18 '14 at 11:36
  • 1
    @MuhammadBabar Thank you a lot! Maybe this answer does not match 100% to the question but it is exactly the elegant and simple way I was originally looking for! – tbolender Dec 15 '14 at 21:23
10

If you want Up to do exactly what Back does, you can do this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}

Note that the default implementation of onBackPressed() just calls finish(), but onBackPressed can be overridden.

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
8

I think a better solution can be found in this post.

Calling finish() works in specific situations but may not always produce the behavior described in the documentation e.g:

enter image description here

By calling

Intent intent = NavUtils.getParentActivityIntent(this); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 
NavUtils.navigateUpTo(this, intent);

you'll return to the parent activity in the state in which you left it. If you have a flat app structure, it'll still act just like the Back button.

Community
  • 1
  • 1
the911s
  • 1,824
  • 17
  • 17
  • 1
    This one should be the accepted answer, because it will cover all possible cases, Thanks @Andy – AbdelHady Jan 29 '15 at 15:17
  • 2
    This is the same as setting `singleTop` in the manifest, only programmatically. Good answer – avalancha Oct 09 '15 at 14:59
  • 1
    Although your diagram makes a lot of sense, latest Gmail app as of 23rd August 2016 has the ActionBar/ ToolBar back arrow and the system back button behave all the same – ericn Aug 24 '16 at 06:41
0

for a real "home" functionality , you should see the api demos ,under "App/Activity/Reorder Activities" .

the reason : what if you have something like this : activity1->activity2->activity3 , and now you wish to go to activity1 by pressing the home button on the action bar?

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • I only have two activities, the main and a detailed one. I always want to go back to the main activity from the detailed activity. I just like having the Actionbar home button as another (intuitive) method to go back to the "home" screen, in this case the main activity. – mraviator Apr 25 '12 at 17:19
  • 1
    even so , you can reduce the time of opening the second activity by not closing it – android developer Apr 25 '12 at 18:42
0

I believe the simplest way is to override the "getParentActivityIntent" method of the detail activity adding the flag "Intent.FLAG_ACTIVITY_CLEAR_TOP":

@Nullable
@Override
public Intent getParentActivityIntent() {
    Intent intent = super.getParentActivityIntent();
    if (intent != null) {
        return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }
    return intent;
}
Marcelo C.
  • 3,822
  • 2
  • 22
  • 11
0

There is another solution which can be in hand for somebody. I had the same double-behavior when user pressed Back and ActionbarBack buttons. I was fine with Back btn behaviour. I didn't want an activity to be recreated. So I overrode the method

     @Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

Works fine for me

Lanitka
  • 922
  • 1
  • 10
  • 20