0

It's necessary for me, because I use a navigation drawer in my app, and I don't want each time when I click the item it starts a new activity for me.

For example, there are 2 items in the navigation drawer, A and B. First I click A, and it starts A-activity. Then I click B, and it starts B activity. And finally I click A again, and I want to return to the last A-activity and its states instead of starting a new A-activity. How can I make it?

Thanks very much!

Niubility
  • 577
  • 5
  • 19

3 Answers3

2

Let's say you have 4 activites like so:

A --> B --> C -->D

where D is the topmost activity and A is root activity. If you are 'falling back' to activity B, then you'll need to do two things in order to avoid hitting B's onCreate method.

1.) Make B a "SingleTask" activity. You can do this in the Android Manifest. To be brief, this means that only one 'instance' of B will ever exist within this Task. If B is already running when it's called then it will simply be brought to the front. This is how you do that.

  <activity
    android:name=".ui.MyActivity"
    android:launchMode="singleTask"/>

But you don't want to just bring B to the front. You want to 'fall back' to B so that your stack looks like

A--> B

2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed.

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed. B will not be recreated, it will only call onNewIntent.

Arjun Issar
  • 672
  • 4
  • 13
  • 32
1

you can set Activity Aas a single instance and when you want to start that use FLAG_ACTIVITY_REORDER_TO_FRONT like this :

in manifest:

  <activity
        android:launchMode="singleInstance"
        android:name="com.test.activity_a"
        android:label="Activity A" />

in code :

Intent intent = new Intent(context, cls_a);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mehd azizi
  • 594
  • 1
  • 5
  • 16
  • You don't need `singleInstance` here, what it does - only creates a task with a single activity in it, not helping the problem at all. But! `FLAG_ACTIVITY_REORDER_TO_FRONT` - using the flag looks like good idea (: – aleien Oct 03 '16 at 10:50
1

It depends on what is your intent to do with your navigation.

Launch mode won't be able to help you much, unless you have only one activity you want to save and not recreate, in this case you may try using singleTask launch mode - it creates new task with it's own backstack and ensures that your activity appears only once in that task - I don't recommend doing that, unless you really know what and why you are doing. Read this for more deep insights about different launch modes. Also, don't use singleInstance - it just creates new task with a single activity at the root, you'll still need to understand how to switch to the correct task.

You may try using Intent.FLAG_ACTIVITY_CLEAR_TOP when starting and activity, but your other activities, pushed from backstack will still be recreated, and I guess it's not your intent.

What am I proposing - use one Activity and switch Fragments instead (see Handle Navigation Click Event section). It's highly recommended way to implement screen switching with navigation drawer (and you'll experience much less pain and see that screen switching is WAY FASTER!). You'll be able to save fragments instance states, and setup fragments with saved bundles (not to mention that you'll be able to use single navigation drawer at the root, not one per activity.

Update: using FLAG_ACTIVITY_REORDER_TO_FRONT may help you, but need more research.

Cheers!

aleien
  • 786
  • 1
  • 9
  • 22
  • Yes, I use flag FLAG_ACTIVITY_REORDER_TO_FRONT and achieve my goal. Do not need to set singleInstance or singleTask in manifest. Now there's only one A-activity in the activity stack and when I start it in other activities, it will be just put top of the stack and other activities that were once above it will not be removed. However, I think using Fragments is exactly the kingly way, for there's some flaw in my app now. When I take A-activity out again, the screen appears a little flicker. – Niubility Oct 09 '16 at 05:23