Clearing the Launcher Activity from the stack - android

In my app, I have activity A (Launcher/Main Activity) ,B,C. A launches B or C depending on if the user is authenticated.
Now how do I remove A such that when the back button is pressed it just exits the app, not to A. Setting the flag FLAG_ACTIVITY_CLEAR_TOP doesn't seem to work.
#Override
public void onBackPressed{
super.onBackPressed();
finish(); // finish activity B or C
//also finish activity A
}

I think you need to set
android:noHistory="true"
on A. You might also need to launch B/C with these Flags:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Finally, you can finish all activities under the current one in the task stack with the same affinity by calling:
finishAffinity()
For further information: Android: Clear the back stack

Related

Back without destroying an activity

I have A and B activities. A is singleTop on launch, B is excludeFromRecents. When I back from B to A, B is always destroying. How to prevent it? When I press Home then B is just stopped and not destoying. Thanks.
Following back code if B activity:
Intent intent = new Intent(this, ActivityPlaylist.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Without flags back not working at all.
I also have prevention of creating new instance in onCreate of A reason of I launch app from Eclipse (bug):
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
finish();
return;
}
Upd: my case is I need do not lose B condition, including list position and so on.

Finishing multiple activities to resume an old one in stack?

I need to finish multiple activities in stack and resume an old one. For example,
There are four activities in stack Act1, Act2, Act3 and Act4. On some event in Act4, I need to finish Act4, Act3 and Act2 to resume Act1.
I tried using Intet.FLAG_ACTIVITY_CLEAR_TOP but it's recreating the activity which is not my requirement.
Advance thanks for suggestions.
You have used Clear_top, it will clear all the activities and bring act1 to front, but, onNewIntent method will be called of you activity, you can perform any task you want in that, after activity starts again.
Or, you can use flag FLAG_ACTIVITY_REORDER_TO_FRONT with your intent, it will do:
FLAG_ACTIVITY_REORDER_TO_FRONT do this: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.
FLAG_ACTIVITY_CLEAR_TOP will do: 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.
for more information on flags look
http://developer.android.com/reference/android/content/Intent.html
There are four activities in stack Act1, Act2, Act3 and Act4. On some
event in Act4, I need to finish Act4, Act3 and Act2 to resume Act1.
Do this:
Intent intent = new Intent(Act4.this, Act1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
use android:launchMode,will be ok.
<Act1
...
android:launchMode="singleTask">

How to remove all activity in Stack when press back button

I have a list of activities A - B -C -D - E and more, for example final activity is K. I want clear all these activities in stack when i press BACK button. How can i do ? In fact, i over ride
onBackPress(){
moveTaskToBack(true);
finish();
}
but only current activity is deleted and application exit. Then, i come back application, it resume activity before K. I want it start from begining when i re-open app. I think the reason here is because the list of activities in stack still are stored, so i want to clear all stack when clicking BACK button. Any suggestions ? thank you very much !
There is method called finishAffinity() for finishing all activity.
public void onBackPressed(){
super.onBackPressed();
this.finishAffinity();}
You need to call your activity with the FLAG_ACTIVITY_CLEAR_TOP inside your onBackPressed
#Override
public void onBackPressed()
{
Intent it = new Intent(YouCurrentActivity.this, YourFinalActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
Hope it Helps!
Either use the noHistory flag in the manifest or finish each activity yourself when the user navigates away.
startActivity(myIntent);
finish();
Another solution, maybe the best, if you have so many overlaying Activities: use only one Activity and handle the content in Fragments. This way you are in control what exactly you want to show when the user hits the back button.
In API level 11 or greater, use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flag on Intent to clear all the activity stack.
Add this code on your onBackPressed() method,
> This launch mode can also be used to
good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to
start the root activity of a task, it
will bring any currently running
instance of that task to the
foreground, and then clear it to its
root state. This is especially useful,
for example, when launching an
activity from the notification
manager.
So your code to launch B would be:
Intent intent = new Intent(A.this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity

Not to destroy current activity on back button is pressed

I have two activities, A and B. I have a button in A,when pressed starts a new intent to B.
However whenever I press the back button while in B, than click the button again is restarts Activity.I do not want to do that,I want it to resume Activity B.
Actually I am doing some networking in Activity B and I want to save unless the user wants to refresh.
How can I do it? Thanks in advance
Use
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
Hope, It must help you
You need to Overrider the
onBackPressed
method and start there the activity like this:
#Override
public void onBackPressed() {
Intent intent = new Intent(this, activityA.class);
startActivityForResult(intent, PICK_CONTACT_REQ);
}
This sounds like you need to rethink your architecture. You say:
Actually I am doing some networking in Activity B and I want to save
unless the user wants to refresh.
If this is the case, you probably don't want to do your networking in ActivityB. Maybe you should start a Service from ActivityB to do the networking. The service and the activity can communicate with each other so that the activity can keep the user up-to-date about the state of the networking. If the user presses BACK in ActivityB, it can finish (as usual) and return to ActivityA. In this case, the Service is still managing the networking. When the user again starts ActivityB (from ActivityA), the new instance of ActivityB can communicate with the service to see if there is any networking going on, and if so it can get the current status of that networking or start it or stop it or whatever.
I guess I'm too late but I had a similar problem.
I had two activities A,B and a next button in A.
Whenever I tried to do: A->press next button ->B->press back button->A->press next button->B, B screen got destroyed when I pressed the back button. So when I came back to B for the second time it was a newly created B (all the information I had put in was gone).
So it was like A->B->A->new B when I just wanted to go back to the original B! :(
So what I did was, in activity B, I overrode the onBackPressed() function so it doesn't destroy the activity. I also set the flag so that if there is a A activity already running, it would just pull it up to the front instead of creating a new A activity.
#Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
Then, for the onclicklistener function for the next button in activity A, I set the flags similarly.
public void onClickNextbutton(View v){
Intent intent = new Intent(getApplicationContext(), ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}

Clear all activities in a task?

I have a splash screen activity, then a login activity. My history stack looks like:
SplashActivity
LoginActivity
when the user successfully logs in via LoginActivity, I want to start WelcomeActivity, but clear the entire stack:
SplashActivity
LoginActivity // launches WelcomeActivity ->
WelcomeActivity
// but now all three are in the history stack, while I only
// want WelcomeActivity in the stack at this point.
Is there some flag I can use to do that?
// LoginActivity.java
Intent intent = new Intent(this, WelcomeActivity.class);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
Not sure if using the FLAG_ACTIVITY_CLEAR_TASK will clear out all activities in my task or not. I can do this 'manually' by unwinding the stack by using startActivityForResult() calls, but will be more fragile and more code to maintain.
Thanks
Yes that should work fine. You could use:
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK
which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Welcome activity -> Activity A and then you want to get back to Welcome from A, but the extra flags shouldn't affect your case above).
Intent intent = new Intent(this, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Use android:noHistory="true" on the splash activity in the manifest file.
<activity
android:name=".activity.SplashActivity"
android:theme="#style/theme_noActionBar"
android:noHistory="true">
finish() removes the activity from the stack. So, if you start LoginActivity and call finish() on SplashActivity, and then you do exact the same to start WelcomeActivity you will get the desired behaviour. No need to use extra flags.
Intent.FLAG_ACTIVITY_NO_HISTORY can work in your case too if you do not want the activity on the history stack.
Just do this to clear all previous activity in a task:
finishAffinity() // if you are in fragment use activity.finishAffinity()
Intent intent = new Intent(this, DestActivity.class); // with all flags you want
startActivity(intent)
In case that all of three activity is involved in the same app(same taskAffinity), you can pick either 1,2 or 3 below. otherwise you should pick 1,2 below.
If you don't want to return back SplashActivity from LoginActivity, you can define activity attribute noHistory in AndroidManifest.xml or you can set FLAG_ACTIVITY_NO_HISTORY into the intent to launch SplashActivity. if SplashActivity is started from Launcher, you should pick way to set activity attribute noHistory.
If you don't want to return back LoginActivity from WelcomeActivity, you can use either activity attribute noHistory or FLAG_ACTIVITY_NO_HISTORY like number 1 above.
If you want to clear back stack on specific situation, you can use FLAG_ACTIVITY_CLEAR_TASK in conjunction with FLAG_ACTIVITY_NEW_TASK(FLAG_ACTIVITY_CLEAR_TASK always must be used in conjunction with FLAG_ACTIVITY_NEW_TASK). But, if the activity being started is involved in other app(i.e different taskAffinity), the task will be launched other task after the task is cleared, not current task. so make sure that the activity being launched is involved in the same app(taskAffinity).

Categories

Resources