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!
Related
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);
I searched a lot of threads and implemented a lot of solutions but I think I want something else.
Suppose I have A->B->C->D activities. From activity D I wont to go to the same instance of A and also clear D,C,B activities. I want to resume activity A and not recreate it! How can I do that? But remember, I want also to clear all the other activities, so I in the stack I should have only activity A.
Thank you! :)
just do on back button , Try to call your intent with the flag "BROUGHT TO FRONT"
Intent intent = new Intent(this,A.class);
intent .setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent );
Assuming activity 'A' is the root of the task of your application - Using the flag CLEAR_TASK along with NEW_TASK will portray the same behaviour as yours. All you need to do is add these flags in the intent for activity 'A'.
The flag CLEAR_TASK will remove all the activities on top on 'A' and NEW_TASK will create a new task with 'A' (same activity instance) as its root.
Hope this helps
I'm just learning Android programming. The way I understand it is that the services work like a stack, Is there a way for a activity to return to the first activity that started the app, instead of just the previous one.
Example, say I have 4 activities, a,b,c,d. Is there a way for activity d to have a button that would bring up activity a?? Instead of activity d going to c, and c going back to b???
You can start again activity a, but using a flag in your intent:
FLAG_ACTIVITY_REORDER_TO_FRONT
From android docs:
If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.
Using this, you'll reuse the instance of activity a already running, instead of starting a new one. Note that doing this, the instances of activities b, c and d will remain in the back stack (now after activity a).
Otherwise, if you want to finish this activities (and remove them from the back stack) you can start activity a (from d) with this other flag:
FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
EDIT: this is a good resource to read, if you haven't did it yet:
Tasks and Back Stack.
Yes, you can do that. You simply code an intent for activity a to the button.
Use the command
finish():
You will be reverted to the main page
or else write an intent pointing to a
You can send information to different Activities via Intents.
Intent myIntent = new Intent(this, AvitivityName.class);
startActivity(myIntent);
Don't forget to add your Activities in AndroidManifest.xml.
Here's some tutorial on Android Intents:
how to use Android intents.
how to switch to another activity with a button click.
How can I restart An Activity on click?
For exemple: I have got in my AndroidManifest.xml 2 activities the activity A and B and they start when application starts...
But what I want is when I click in a button that is on Activity A it must restart activity B.
when you are in activity A, and proceeding to activity B, then your activity B automatically starts/re-starts
Why do you need to start both Activities on starting your app? When you say "restart", do you actually need to stop Activity B and start Activity B again? Or do you just want to show it? To start an Activity from another Activity, you could call something like this:
startActivity(new (Intent(this, ActivityB.class)));
The Android documentation gives plenty of detail. However, I think you should consider why you are starting two activities at once, and whether you might want to use a Service instead (not knowing any details of your app, I can't say).
Intent intent = new Intent(CurrentActivity.this, ActivityToLaunch.class);
startActivity(intent);
call above piece of code on onClick of view method.
I have a main activity A. There are two scenarios
1) A launches B.
B has launchmode singleTask and is launched with FLAG_ACTIVITY_NEW_TASK.
now I have a menu option in B which performs a delete operation and starts the activity A.
2) A launches B, which launces C it also contains the menu option to perform delet opereation.
I want A to be started with clearing the stack in both the scenarios but the activities belonging to another task still present there
I am stuck is there a way to clear the stack.
try using following code on delete opereation on both B and C activity
Intent intent=new Intent(B.this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If i talk about your second scenario then FLAG_ACTIVITY_TOP_CLEARwill clear the stack..
And your stack now will be only A instead of A-B-C-A.
and in second case AFAIU your problem you have only two activity A and B so if you want to restart A then after restarting A manually finish B.
Hope you got some trick.
Another thing if you have activity with launchmode=SingleTask then you can use flag_activity_brought_to_front.
That will act like
A-B-c to A
Why not finishing B and C before calling A again?
finish();
My method can not meet your goal A to be started with clearing the stack,But when user choose delet opereation and start B from A again the task that include A and B will be reset.
Use the flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET to mark the task will be clear when needed when you first start activity B from A, then if user choose delet opereation menu item from B or C, you set a flag, so next time from A(A should be single_task launch mode in manifest), you use the flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED to start B. This flag will clear B and all top of the B.
Hope this will help you :)