Understanding Intent.FLAG_ACTIVITY_CLEAR_TOP - android

As per the image shown above, I have some queries. It is requested to read each step in order :-
Each block is an Android Activity
Arrow represents the Stack Direction - the order in which activities are opened(A is started when the app was first launched)
Here when the User reaches ACTIVITY F and want to open activity Z (We are using Flag_Activity_clear_top) for the same.
After that from ACtivity Z when the user wants to open the Activity D.
****Our Requirement at this step is - When the Activity D is opened and the user do presses the back button - I WANT THAT USER SHOULD BE REDIRECTED BACK TO THE ACTIVITY C, AFTER THAT ACTIVITY B and so on..** **
Currently when we press back from the activity D(after coming from Z), then we are being redirected to the Activity Z.

CLEAR_TOP isn't good, because if you open an activity that way, it will remove the whole stack and that doesn't sound like what you want.
Try this:
When starting activity E (from D), F (from E) and Z (from F), do it with the flag "FLAG_ACTIVITY_NO_HISTORY". This flag will prevent the new activity to appear in the back stack.
Keep in mind that any activity you open this way will not be registered in the back stack. So, if you hit back while (for example) you're in F, it will return to D.
Hope this helps!

->Incase anyone is facing the same issue. Try sending the intent along with the flags 'FLAG_ACTIVITY_CLEAR_TOP' and 'FLAG_ACTIVITY_SINGLE_TOP'.
->Example mentioned in the docs: link
Consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receives the sentIntent, resulting in the stack now being: A, B.

Related

Keep one activity in the bottom of the backstack

I want to learn the proper way to manage the activity back stack with regards to my issue. Most of the time when a person uses my app, I want to keep an activity in the bottom of the stack, let's call this Activity A. This would be their "Home" activity. I have a navigation view which can take the user to a bunch of other activities, but I want to manage what displays when they tap back. I want Activity A to always be the last activity in the stack, so the stack can look like A -> B -> C-> D, and when the user is on Activity D and they want to go to Activity E, I want the stack to look like A -> E when they press it.
A possible solution I have found is by clearing all the activities in the current stack, launching Activity A, and sending an intent for Activity E in the intent I launch Activity A with, then that will just check it's intent extras and if it finds an intent in the extras it would just launch that intent. This results in the stack looking the way I want, Activity A -> Activity E. I just want to know if there is a better or simpler way.
I have tinkered with the activity properties in my manifest, but it seems like I can't do exactly what I would like to with those.
Any help would be appreciated :)
Lets assume you want to keep activity A in the back stack so that whenever a user presses back, you want to show activity A on top. Lets say you go to B from A and then C from B. So whenever you go from any activity(other than A) to any other activity just call finish() from the calling activity, this will remove the stack entry of the corresponding activity, ensuring that only activity is there in the back stack.

clear back stack of activities

I have some activities A, B, C, D. No the way it's set up is like this.
When user starts the app, activity A starts.
Based on a preference, which if set to true, immediately starts activity D
From there D starts C and C starts B which then starts A
... At this point i want D, C, B removed from the back stack so that user cant go back to them by pressing back from A (but the back button should work like it should when in D, B, C).
so to sum it up i need something like this
D <--> C <--> B --> A
I tried using intent flags Intent.FLAG_ACTIVITY_CLEAR_TOP & Intent.FLAG_ACTIVITY_NEW_TASK
but they dont work.
How do i accomplish this??
Then start activity A with FLAG_ACTIVITY_TASK_ON_HOME flag set.
Ok.
How about calling finish on B after launching A?

Android: prevent activity to be instantiated more than once

I was going through Android Task and Back Stack documentation and at one point they mention this:
if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state)
Let's take an example:
I have Activity A Starting Activity B which Starts Activity C which starts D.
Stack is A->B->C->D now it is possible to Start C from D so when we start C from D stack will be
A->B->C->D->C
Now instead of this standard behavior I want Activity to have only 1 instance or only 1 entry in the Back Stack. "SingleTop" will not work since Activity C was not on top when we started it from D.
I might be missing something but is there any way to achieve this making sure an activity has only 1 backstack entry?
Thanks
Pranay
Use Intent.FLAG_ACTIVITY_CLEAR_TOP, e.g.:
Intent intent = new Intent(context, <your_activity_here>);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
So, all the activities in stack after activity C will be finished automatically. If you use the specified flag
A->B->C->D
will become
A->B->C
You can also use android:launchMode="singleInstance" in your activity tag in manifest

Getting back to the first activity via intent, what happens to the activity stack?

I have an application which pulls data from a webserver, putting this data in a listview and then presenting it to the user. There are 4 activities involved, which can be called like this:
A -> B -> C -> D
or
A -> B -> D
Basically all the activities except A are pulling data from the web. Should there be any problems with the connection and there is a timeout coming up I want the activities B, C and D to inform the user and get back to A.
So what I did right now is, I set A to launch mode singleTask. This way I can catch the timeout exception and call a new intent starting A. But what happens to the activities in between? Let's say I am calling A->B->C->D and then in D the connection times out. Now the app is going back to A, but what about B and C? Does android automatically call onDestroy on these? What happens to the activity stack? Any hints appreciated.
Cheers
When you launch activity A, from activity D, set the intent flag:
FLAG_ACTIVITY_CLEAR_TOP
Using this flag will clear any activities in between A and the activity you are in, bringing A to the front. You also probably don't need to be using singleTask as a launch mode.
Let's say I am calling A->B->C->D and then in D the connection times out. Now the app is going back to A...
With your example, if all the child activities are launched with startActivityForResult() and activities B & C implement:
finishFromChild(Activity activity) {
...
finish();
}
When activity D calls finish() after it has timed out, then each child will close in order (D -> C -> B -> A) with a chance to return any relevant data you might want to salvage.

Moving between activities: killing two previous activities

I'm developing an Android 2.2 application.
I'm very new on Android, and I see that if Iaunch a new Intent from an activity, this activity goes to paused state.
If I want that user can't goes back to this previous activity, what must I do? May I kill this previous activity with finish?
UPDATE
An example:
A, B C and D are activities.
A is the first activity.
A launches B, and B launches C, and C launches D.
I want to close or kill activity B and C when D is launched.
Thanks.
You can use your intent to startActivity like normal and follow the startActivity with
finish();
If you do this on your hypothetical activity B, you can hit the hardware back button in C and it'll take you directly to A.
You might want to rework your design. What if you have A call startActivityForResult(B, 0), and when B would have launched C, instead setResult and finish. Back in A, onActivityResult gets called, with the result you set. You can use that result as a message for A to now start the activity C.
See if that helps - http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP (This and other flags)
And also you must invoke
finish();

Categories

Resources