Currently I have 3 Activity classes A, B and C.
Activity A is a singleTask while other has the default launch mode.
Consider a case: the user is first in A, then starts B, and then starts C.
The back stack now is ABC.
Next, the user starts A again.
The back stack now is A, but what I would like to be achieved is ABCA.
I know not setting Activity A to be singleTask can have a back stack : ABCA.
But I really need the Activity A to be the same instance.
Anyone know how to do this?
You have stipulated two conditions:
what I would like to be achieved is ABCA.
and
I really need the Activity A to be the same instance.
These two conditions are mutually contradictory. Absolutely.
What you want is impossible.
That is all.
Are you sure you need singleTask and not singleTop ? Could you describe why do you need it singleTask ?
That is not possible.
if you want to open existing activity then
launch your activity as
Intent intent = new Intent(this, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Related
I am having two activities, Activity A and activity B.
Activity A starts activity B.
So, the activity stack after some interactions will look like A -> B -> A -> B.
The problem:
I need to go the first activity A in the stack from activity B (last B in the stack). I am using FLAG_CLEAR_TOP as well as Intent.FLAG_ACTIVITY_NEW_TASK for achieving the same.
Right now, the activity A (stack pos 3) will be shown from activity B, but when I press back button, the activity B will be shown again (since activity B (stack pos 2) is already there in the stack).
How do I overcome this issue?
PS: I tried using launchMode singleInstance and singleTask for activities A and B, but that solution doesn't work for my app.
Thanks in advance.
Add finish (); after switching activites like
Intent intent = new Intent (activity_a.this, activity_b.class);
startActivity(intent);
finish ();
Avoid creating multiple instances of activities if possible. Android isn't designed to allow you to identify (and return to) a specific instance of an Activity in the stack, if you have multiple instances of the same Activity in the stack.
Depending on your application, there are different ways to solve the problem.
One way that may work for you is to use <activity-alias>, which allows you to reuse an existing Activity implementation by another name.
Another way would be to create another Activity class, that just inherits from the original, so that you have 2 classes with exactly the same code, but with different names.
The best way is to rearchitect your application so that you only ever have one instance of each Activity alive at any given time. You can do this by rearranging the task stack using Intent.FLAG_ACTIVITY_REORDER_TO_FRONT.
I'm new to Android development and here's my problem:
I have 2 activities: A and B.
From A, I call B via startActivity(new Intent(A.this, B.class))
From B, I do some things and return to A the same way: startActivity(new Intent(B.this, A.class))
However, when on new-A I press "Back" button, first I see B class and then the old (unchanged version) of A class.
I've tried placing finish(); inside onPause() method. However, then my Activity crashes on orientation change.
How can I properly control activity backstack? Thanks in advance.
When you call startActivity without any additional flags, you will create a new instance of the Activity specified. This means that your back stack ends up looking like this:
A -> B -> A(2)
You have two options:
First option, you can add flags on the Intent that instructs it to reorder A to the front if it is already present in the back stack. Note that this will not close B; you have to call finish() in B afterward.
Intent intent = new Intent(B.this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startAcvitiy(intent);
finish();
Second option is to use startActivityForResult instead of startActivity in A and have Activity B pass a result back to A. I think this is much cleaner and it gives you a very easy way to react to what happened when B was finished (e.g. you can update your UI). See this guide from the Android developers site.
Either StartActivityForResult or think about using fragments. If you need to jump between activities think about why you are doing this? If you are passing info between the two then there are a couple of options. Fragments give you the option and you can hold the data on the Activity that hosts the fragments or you can have a Singleton living in an Application class that you would extend.
Read about activity lifecycle - here. You have to implement saving of instance.
It sounds like that rather than calling startActivity in to go back to A, you can just call instead finish();. This will land you back on your previous Activity.
Alternatively you can accomplish the same if you use FLAG_ACTIVITY_CLEAR_TOP:
Intent intent = new Intent(B.this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Regarding the accepted answer's suggestion to use FLAG_ACTIVITY_REORDER_TO_FRONT - for your case it really doesn't matter. but in a slightly different case where you have A->B->C and you use REORDER to go back to A, you'd actually end up with a stack that looks like B->C->A. With CLEAR_TOP, you'd end up with just A. Point is, REORDER is meant to REORDER, CLEAR_TOP is supposed to pop the backstack. In your simple case they happen to accomplish the same, but in my opinion it's better practice to use the flags as they're intended.
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
I got 3 activities in my app. Activity A,B and C.
I am in activity B now and I need to know if there are more activities in my activity stack or when I finish activity B I will exit the app.
I found ActivityManager that can do the job, how ever it require GET_TASKS permision.
Is there any other way for doing it?
While going from one activity to another activity you can set flags clear top to delete other activity those are now on stack and then go to new activity. If you do this then finish the current activity then you are out of the app.
For example
Intent imra = new Intent(getApplicationContext(),
MoreActivity.class);
imra.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(imra);
finish();
Unfortunately the answer is just no!
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