clear/finish all activities including SingleInstance activities from the stack in android - android

I have three activities namely A, B, and C. A is launcher activity while B and C are singleInstance activities. The sequence of movement between activities I want is A(press button) -> B and then B <-> C (i.e, B and C keeps alternating as many times as they want). When in B, pressing navigation up button takes me to activity A.
Now the problem arises. I am in A now and want to go to B(by pressing button) like I did during start of my App(i.e, I want it to work as if the app has been launched). But it tries to go to activity B and suddenly with a flash it returns back to A. The funny part now is that when I try again(i.e in activity A I press the button again) it goes to B without any problem and the flow works as expected i.e, the flow of activities are same as it was on launch time.
What I want finally?
Once I reach launcher activity which is activity A, I want the app to behave as if it was just launched. i.e all activity including singleInstance activities should be cleared from the stack.
I have tried this in onSupportNavigationUp() method in activity B:
Intent mainIntent = new Intent(B.this, A.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
B.this.finish();
I even tried adding Intent.FLAG_ACTIVITY_CLEAR_TOP also.
But this doesn't help. The problem remains unsolved.
Any kind of suggestion will be greatly appreciated.

Related

clear all activities except one in android

Below I'm showing the flow on how I want to navigate my Activities:
I tried writing the following code inside D and E:
Intent list = new Intent(AddComplaint.this, B.class);
list.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(list);
However, I'm facing two issues:
When B gets launched it shows a grey screen for a while, then it gets loaded.
When I go back from B it exits out of the application rather than going to A (the dashbord).
How can I fix this?
I believe you can achieve what you want by using FLAG_ACTIVITY_CLEAR_TOP. If you send an Intent using this flag, it will be delivered to the existing Activity B, and any activities above B on the stack (C, D/E) will be finished.
Using FLAG_ACTIVITY_CLEAR_TASK will finish all previous activities on the stack, which would make B the only remaining activity - explaining why you exit the app when clicking back. Your grey background is unrelated to activity management and indicates the activity is simply taking a while to call onCreate().
Example code:
Intent list = new Intent(AddComplaint.this, B.class);
list.setFlags
(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(list);
I think FLAG_ACTIVITY_CLEAR_TASK is clearing all the activities from stack.
to go back from activity B to A again, without changing anything to your existing code , simple overrride onBackPress() method in activity B and startActivity A there.

Launching Activities Properly

I have an app that has multiple activities, one activity opens the other and so on. I have a serious problem when it comes to returning to the previous activities. I want to return to the previous activity in the state I left it in(I do not want to recreate the activity). I was able to do so with three activities, but the fourth activity skips the third activity and returns to the second, for example:
Activity A -> Activity B -> Activity C -> Activity D
What I want when I press the back button and the Up button:
Activity A <- Activity B <- Activity C <- Activity D
I initiate Activity A as a "singleTask", then I launch the next three activities like this:
Intent intent = new Intent(context, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
startActivity(intent);
This works perfectly with Activity B and C, but when I get to Activity D and try to return to Activity C, it takes me to Activity B instead of Activity C.
I have been through the internet and I just come seem to really understand the use of Intent flags and activity launch modes. Can someone please assist, pretty please?
Using the launch modes and flags IS the problem. Maybe you'll want to assign activity A "singleTop", but you don't need to use flags when launching B, C, or D (at least not in the situation you're describing). Then pressing back will behave as you expect it to (unless you're overriding it).

Android How to put activity in the background but not in stack

I have activity A and activity B. When app opens activity A opens.
I go to A to B. Then how do you "go back to A from B", without destroying B so that when you go from A to B the process is super fast? (Also how would you have to open old B and not create a new B?)
Also one more question, when you "go back to A from B", is there a way you prevent B to show up when you press back button once you are back in A?
When creating B, do all the hard work in onCreate, so later when resuming it, it starts up faster. B is destroyed only when it's not used and android needs to free some memory. Since it's in the background, switching to it will be pretty fast. To speed it up you can switch without an animation:
Intent intent = new Intent(this, B.class);
startActivity(intent);
overridePendingTransition(0, 0);
To prevent multiple B activities to be created, in your manifest's B activity tags set:
android:launchMode="singleTask"
After you go back from B to A, going back again, will quit the app. That is unless you go to A explicitly. Then you will want to override onBackPressed() in activity A, that always quits the app instead of going back to B.
Think you need to read this: http://developer.android.com/training/implementing-navigation/ancestral.html
If you want to always exit your app when pressing back in A, then you can override 'onBackPressed'.
Regarding how you would keep A 'alive' so to speak, you could set A to be 'singleInstance' or 'singleTask' in your manifest Android singleTask or singleInstance launch mode?

Top-level Android Activities

I'm trying to create a basic app using the AppCompat Drawer, and multiple top-level activities (Not fragments) - I can't quite work out how to manage the backstack - I've tried about a hundred different approaches - but they all require some sort of weird hack to either clear the backstack - or finish the existing activity.
I have 3 activities - A, B & C. A & B are top-level, C is a child of B
I want:
To start Activity A when the app starts
To exit the app when I press the back button from A
To start Activity B from the drawer
To exit the app when I press the back button from B
To start Activity C from Activity B
To go back to Activity B when I press the back button from C
When I start B from B or A from A or B from C by selecting drawer buttons - I should get the top-level Activity back in it's vanilla state.
I have:
protected void startActivity(Class activity) {
final Intent intent = new Intent(this, activity);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
overridePendingTransition(0, 0);
}
Basically I pass in either ActivityA.class or ActivityB.class - with this approach - pressing back from B takes me to A
Using HO_HISTORY, looks ok - but pressing back from C exits the app
Using REORDER_TO_FRONT doesn't seem to do anything??
Using finish() after startActivity works perfectly - unless you choose A or B twice (in which case you exit the app)
Using FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK works perfectly in every manner - except for the super nasty screen flashing as tasks are re-created. And the performance hit...
Help??
Can't you just call finish() right after your starActivity call? (you'd have to remove the SINGLE_TOP flag too -- or you'll run into the behaviour you mentioned when going from B -> B)

How do I clear all Activities from the stack?

I am having trouble popping all activities off the stack using Intent.FLAG_ACTIVITY_CLEAR_TOP and android:launchMode="singleInstance".
In my application activity A, launches activity B (via startActivity) which in turn launches activity C (via startActivity). On activity C the user presses a menu item to return to activity A. When they arrive at activity A, I want only A on the stack such that if they click the back button they return to the home screen (desktop).
This is the code that I am currently using when the user presses a button to return to A:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
All activities are defined as android:launchMode="singleInstance" in the project manifest.
My code doesn't seem to work though. Once I'm back on activity A if I click the back button I return to activity C. Am I misunderstanding how to use Intent.FLAG_ACTIVITY_CLEAR_TOP?
I've always found the best way to ensure C would be removed from the stack is to call finish() after startActivity to remove C from the stack.
The documentation does read as though things would behave the way you expected them to, but it would seem this isn't happening, so finish() will ensure C is removed.
I usually use the technique Al suggested (calling finish() after starting the new activity).
You could also experiment with task affinity. I've never done that myself, but it may be relevant in your case as well. See this thread: http://groups.google.com/group/android-developers/browse_frm/thread/ca3b26a14d024597/129e37375105901b

Categories

Resources