4

I'm developing some application and I have one problem.

I have : 1. Activity A (Navigation Drawer pattern) with ListFragment in FrameLayout: xml:

    <FrameLayout
        ...>

    </FrameLayout>

    <LinearLayout
        ...>

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>
  1. Activity B which shows the detail data of ListView in ListFragment.

How can I go back (using Navigation Up Button) from activity B to Activity A with saving UI of the ListFragment (Activity re-creates if I go back using Home Back). Btw, if I press the back button on my phone, activity does not re-create and returns in previous state.

pharrell
  • 43
  • 1
  • 3

2 Answers2

11

When you use UP navigation, then the previous activity is recreated. To prevent that from happening while you preserve the UP navigation, you can get the intent of the parent activity, and bring it to front if it exists, otherwise create it if not.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent parentIntent = NavUtils.getParentActivityIntent(this);
            parentIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(parentIntent);
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

I also specified launchMode="singleTop" in the Manifest. but I am not sure if that was necessary.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

One thing you can do to prevent the first activity to recreate is by just calling finish() on the second activity when that back button is pressed.

Not tested, but I believe the id is android.R.id.home, so all you have to do is override onOptionsItemSelected in the second activity, like this:

/**
 * Handles the selection of a MenuItem.
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • That breaks "up" navigation though in case you start the app from that activity (using a notification or the like). – EpicPandaForce May 05 '15 at 18:20
  • @EpicPandaForce if I specify a parent activity in the android manifest, that's what it goes back to, doesn't it? – AdamMc331 May 06 '15 at 16:53
  • if you write `finish()` and you start the activity from a pending intent (notification)? I don't think so. That's why the UP navigation was designed in the first place, although it's a bit hectic with how it normally recreates the previous activity for whatever reason. – EpicPandaForce May 06 '15 at 17:03
  • @EpicPandaForce I will test that out, I've never done it before so I don't know the answer. – AdamMc331 May 06 '15 at 17:05
  • 1
    I kinda did, I made it so that the child activity is the one that gets the "launcher" intent. If that is the case, the UP button won't actually redirect properly to the parent, it will just finish the current activity. That is why I came up with the "hack" so to say that I posted above. :) – EpicPandaForce May 06 '15 at 17:06
  • You're welcome! :) To be honest, the biggest surprise for me was that `NavUtils.navigateUpFromSameTask(Activity sourceActivity)` recreates the parent activity no matter what, which *really* isn't what I wanted. So I just did the navigation to the parent manually instead. – EpicPandaForce May 06 '15 at 17:09