Clear all back stacks without intent - android

Is it possible to clear all back stacks without using intent?
I have a situation need to clear all back stacks if user does something in current activity so when back button pressed application should close.

You can use FinishAffinity
From Documentation:
Finish this activity, and tries to finish all activities immediately
below it in the current task that have the same affinity.
in your Currnet Activity: overWrite onBackPressed()
#Override
public void onBackPressed() {
if(condition)
finishAffinity(); //closes application
}
Requires SDK>16

I don't think you can just clear back-stack .
I have a situation need to clear all back stacks if user does
something in current activity so when back button pressed application
should close.
you can set some boolean Flag variable to true when user do that something in that activity and then override onBackPressed()
and in onBackPressed() check for this flag if true the call System.exit(0);
.

If you know the activity should be the only one at the moment you start it, you could use FLAG_ACTIVITY_CLEAR_TASK. From docs:
If set in an Intent passed to Context.startActivity(), this flag will
cause any existing task that would be associated with the activity to
be cleared before the activity is started. That is, the activity
becomes the new root of an otherwise empty task, and any old
activities are finished. This can only be used in conjunction with
FLAG_ACTIVITY_NEW_TASK.
In this case, you should start the activity like that:
Intent i = new Intent(OldActivity.this, NewActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

Related

How to navigate back to previous Activity?

What I am trying to do is, there is no chance the two activities are running at the same time. So I am using for this method in my adaptor class.
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
v.getContext().startActivity(intent);
((Activity)context).finish();
But when I click the back button, it doesn't go to the back activity.
What am I doing wrong?
Basically you should remove finish() method from your code so that it will not destroy that activity and keep it in stack.
Once you call the finish() you can not go back to previous activity. This question explains in details, what happens when you call finish() method.
Remove ((Activity)context).finish(); in your code,
because
here you are finishing your activity which means when you press back you don't have any activity on your stack to go back to.
Finish() method will destroy the current activity. You can use this method in cases when you dont want this activity to load again and again when the user presses back button. Basically it clears the activity from the current stack.
So,no need to use finish() here
Just remove this line:
((Activity)context).finish();
Called the Finish method of activity at button click.
finish() just lets the system know that the programmer wants the current Activity to be finished.
((Activity)context).finish();
That is because you are finishing your activity which means when you press back you don't have any activity on your stack to go back to.
so just remove finish so that i will push WinnerDetailActivity on top of your current activity and on back of WinnerDetailActivity it will open your current activity.
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
v.getContext().startActivity(intent);
And also read about FLAG_ACTIVITY_SINGLE_TOP
If set, the activity will not be launched if it is already running at
the top of the history stack.
//Here intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); will alway remain in intent stack unless you finish it and once this activity is resume.. then it will act as the stack has only on Activity running...
//So just remove ((Activity)context).finish();
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
v.getContext().startActivity(intent);
((Activity)context).finish();
There is two way to resolve this
1.Just remove ((Activity)context).finish(); because Finish() method will destroy the current activity.
2.Just Add below method in your class
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(CurrentActivity.this,DestinationActivity.class));
}
DestinationActivity means Activity where you want to move.
OnBack Pressed Method is used to go back to previous activity,
super.onBackPressed() is used to navigate to previous activity without super.onBackpress it is not possible to traverse to previous activity.
#Override
public void onBackPressed()
{
super.onBackPressed();
}
This can be done in two ways:
Method 1:
Whenever you're stating a New Activity from an Activity, Make sure that you don't call finish(). Calling finish() will destroy the Previous Activity in the Stack.
Next Method is by Overriding onBackPressed(), by doing so you can navigate to the desired Activity.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent( thisActivity.this, yourFirstActivity.class);
startActivity(intent);
finish();
}

Check if a previous activity exists

I have an activity. After checking some stuff, I want to go back to previous activity if a previous exists, and if not I want to start a SpecificActivity. How do i do it?
Edited:
Seems like no one is understanding what I meant, so let me rephrase. Lets say I am in Activity A. I don't know if A is the only activity in the stack. If there are other activities, then I want to finish A and pop the activity right below A in the stack into the foreground. If A is the only activity in the stack, I want to start some activity Z. How do i do it?
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can simply override the onBackpress()
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(this, Destination_Activity.class));
finish();
}
call the next activity like.
Intent intent = new Intent(this,Your_Next_Activity.class);
startActivity(intent);
then it will call your another activity and your current activity will be on background if you will use finish() after calling next activity it will finish your current activity so don't use finish() after calling your next activity in this scenario.
After that when you press back button it will automatically finish current activity.

How to go back to an already launched activity in android

I want to go back to an already launched activity without recreating again.
#Override
public void onBackPressed() {
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
this.startActivity(i);
}
Instead of doing is there any other way to go back to another activity which is already created, which I get back with " Back button "
Add android:launchMode="singleTop" in the FirstActivity declaration in your AndroidManifest.xml like so
<activity
android:name=".FirstActivity"
android:launchMode="singleTop"/>
You could also try the following if SecondActivity was started from FirstActivity
#Override
public void onBackPressed() {
this.finish();
}
Whenever you start a new activity with an intent you can specify an intent flag like this:
// this flag will cause the launched activity to be brought to the front
// of its task's history stack if it is already running.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
or
// If set and this intent is being used to launch a new activity from an
// existing one, the current activity will not be counted as the top activity
// for deciding whether the new intent should be delivered to the top instead
// of starting a new one.
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
When starting SecondActivity do not finish the first activity so when you go back , you will be taken to the first activity.
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
On your second Activity, close the Activity calling finish() instead of creating a Intent for the other screen.
By calling finish() the Application will go back in the Activity stack and show the previous Activity (if its not finished()).
Don't call finish() on 1st Activity, when you start 2nd Activity. When back is pressed on 2nd Activity, it finishes itself and goes back to 1st Activity by default. onCreate() is not called for 1st Activity in this case. No need to override onBackPressed() in 2nd Activity.
However, Android can destroy 1st Activity when you are using 2nd Activity, to reclaim resources, or, if you have checked Don't keep activities in Developer options. In such case you can use onSaveInstanceState() to save data and onRestoreInstanceState() to use it again in 1st Activity. You can also get the saved data in onCreate(). Here is how it works. Also, onSaveInstanceState() is called only if android decides to kill the Activity and not when user kills the Activity(e.g. using back button).

How will I permanently exit my Android app using onBackPressed from my last Activity

I have 8 Activities in my Android app and I want:
1)Every time I press Back button during my first 7 Activities to go back to my previous Activity(Act1< Act2< Act3< Act4< Act5< Act6< Act7) BUT
2)ONLY when I am in the 8th Activity I want to definitely exit my Android app and go to my phone's Home Screen.I try to do it by overriding onBackPressed method in my 8th Activity (Phone Home Screen<-Act8)
I found an Android implementation in which I insert finish();in every intent of all my 8 Activities but this is not what I want since this way I can't go back to the previous Activity whenever I want(with finish(); every current Activity is removed from back stack).
How will I do it please?
My code so far in my 8th Activity is:
#Override
public void onBackPressed()
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
Another way: create a 9th Activity and call it FinishAllActivity or something like that. Make this activity call finish() and then return in its onCreate().
In onBackPressed() in Activity 8, start FinishAllActivity using the FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK flags (see this question for more details). Activities 1-8 will be removed from the stack, then the 9th Activity will start and immediately terminate and your task stack is clear. When you reopen the app it should start from Activity 1.
The advantage of doing it this way is that you don't have to modify Activities 1-7.
Add a public static boolean to one of your classes that indicates the app is exiting. Set this boolean in activity 8 when you want the app to finish, and have all of your other activities check it in their onResume() and finish immediately if it is true. Make sure the first activity clears it before finishing, or it may still be set the next time the app runs. (Android doesn't necessarily discard the VM when your last activity finishes, so the class and its static members may be reused next time.)
Note that this is the simple way, not the "Android way." Global variables are generally frowned upon, for reasons you can Google. The "correct" way to do this would be to start each activity for result and return a result to onActivityResult(...) that indicates whether the app is exiting.
You can implement a broadcast receiver and have each of your Activities that you want to close call finish() when they receive the broadcast (which will be sent from your last activity). I would imagine you'd need to have your broadcast receiver class be either an anonymous inner class or a private class within your activity(s) so that you can easily access your enclosing Activity's finish method.
Here's a good example of broadcast receivers:
http://www.tutorialspoint.com/android/android_broadcast_receivers.htm
Look at the custom intents section.
Doing it this way is a loosely coupled way to implement what you are looking to do.
use FLAG_ACTIVITY_CLEAR_TOP Flag in your intent like below example.
Intent intent = new Intent(getApplicationContext(),FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
in your first activity check below condition.
if (getIntent().getBooleanExtra("EXIT", false)) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
}
here FLAG_ACTIVITY_CLEAR_TOP work like below example
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 receive the given Intent, resulting in the stack now being: A, B.
so here you have to call D is your last activity and A is your first activity.
This way you are finishing your 8th Activity returning to your 7th Activity and same time you are like emulating a pressing of Home button on a device. When you rerun your app it will appear with 7th Activity on a screen. If you wish to see the 8th Activity in this case then just remove the finish() method. If you wish next time your app to start with 1st Activity then you should finish all the activities from 8 to 2nd but not the 1st. Also you could launch your 1st Activity adding a FLAG NEW_TASK or some other flags.
UPDATE
My advise (for the quick result without changing the workflow) is to use startActivityForResult() to start all activities in chain. When user exits the app just return a special parameter using setActivityResult() to get all nested activities know about user's choice making all nested Activities to run finish(). This way all your 8 activities will be finished properly.

Issue with activity called with Intent.FLAG_ACTIVITY_CLEAR_TOP

I want to finish my app calling the first activity with Intent.FLAG_ACTIVITY_CLEAR_TOP and finishing it. However, when it finishes, the app restarts automatically, and goes directly to Activity 2.
Why? Isn't the stack of activity supposed to be empty after finishing an activity called with Intent.FLAG_ACTIVITY_CLEAR_TOP?
My stack is Activity2>(more activities)>Activity1.
In Activity2
Intent exit_intent=new Intent(context, Activity1.class);
exit_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
exit_intent.putExtra("EXIT", true);
context.startActivity(exit_intent);
In Activity1
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
From the javadoc:
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.
CLEAR_TOP will wipe out all Activities ABOVE Activity1: if Activity2 is below Activity1 then once Activity1 finishes you will see Activity2.
Are you sure finish() is being called properly in Activity1? I'm getting the feeling it's not, because:
I'm not sure where that if statement goes inside your Activity. It should be in onNewIntent.
If you are making that if statement inside the onNewIntent method, it's still wrong. The docs specify that getIntent() will always return the original intent that started the Activity, unless you call setIntent().
To conclude, maybe something else is getting called in your Activity1 (can't tell without the full code) that starts Activity2 instead of finishing.
If what I described is not the case, and your activity stack indeed looks like Activity2 > Activity1 like the others have described yes, it will not work. Just call finish() in Activity2?
According to the docs:
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.
So it doesn't clear the entire activity stack, only any other activities that were on top of an old instance of the activity being launched.
You have it right but are you catching the intent inside onNewIntent method of your first activity? also activity 2 should be launched after activity 1 in order for this to work.
From Intent doc:
public static final int FLAG_ACTIVITY_CLEAR_TOP
If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created.

Categories

Resources