Finish all activities - android

I do start an activity A using intent from within aPreferenceScreen. What I do want to do is pretty simple. Depending on some actions the user may take I want to finish all previous activities and start the LoginActivity. Something like restarting the whole application.

Use Intent.FLAG_ACTIVITY_CLEAR_TASK flag in intent to clear all the activities in the stack
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);

Just use finishAffinity() after startActivity().

Just use finishAffinity() after startActivity()

Related

Remove all activities of an app from every existing task in Activity Back Stack

Is there a way of removing all activities belonging to the app in foreground (my app)?
The activities might be present in different tasks. Also after removing all activities my app should return to home screen which is launcher activity of my app.
Any kind of help would be greatly appreciated.
You need to call your activity like this.
Intent login=new Intent(MainActivity.this,HomeActivity.class);
login.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
login.putExtra("flag",false);
startActivity(login);
finish();
You may need
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TASK
Any existing task that would be associated with the current activity to be cleared before the desired activity is started. Simply old activities are finished.
FLAG_ACTIVITY_NEW_TASK
If set, this activity will become the start of a new task on this history stack.
for more details, Intent Flags
Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();

Why does FLAG_ACTIVITY_CLEAR_TOP not work?

As the title says, Why intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) or intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) won't work?
I have 3 Activities let us say A, B and C.
When I am trying to launch Activity A from C with code:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
It simply starts Activity A but doesn't clear the top.! -_-
I have also tried using setFlags().
I read different questions on SO about this problem, but I couldn't find the right answer. >_<
Somebody please help!
Edit
Code for onBackPressed() in activity 'A' as requested by #codeMagic.
#Override
public void onBackPressed(){
if(wvLogin.canGoBack())
wvLogin.goBack();
else
super.onBackPressed();
}
From the documentation for 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.
As you added in your comment, the activity A has been finished before calling B, so this situation doesn't apply. A new instance of activity A will be launched instead.
As I see it, you have two options here:
1) Use the Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK flags. This will start activity A as the root of the stack. It works, but any other activities in the stack will be lost. Assuming A was the first activity (or at least, that you are not interested in any previous activities in the task stack) then it doesn't matter. Note: CLEAR_TASK requires API Level 11.
2) Another possible solution (in case the previous assumption is not true) would be to not use intent flags at all:
B starts C with startActivityForResult().
Instead of calling A, C finishes, having set a result for B indicating that A must be launched.
In B.afterActivityResult(), finish B and launch A.
You are missing the Intent.FLAG_ACTIVITY_SINGLE_TOP flag
Try this:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
You used a diferrent intent: use the one you initialized:
Intent i = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); \\WRONG;;
startActivity(i);
solution:
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); \\RIGHT;;
You could either put a noHistory true to the Activity A in the manifest
android:noHistory=true

Which intent flag do I need to use so that When When I press back I don't see that activity again?

I have an activity that leads to another. i want to make it so that the first activity never opens again once it directs to the other one
clear_top doesnt work. which flag is needed?
This will start a activity with new information and no history
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
MainActivity.startActivity(intent);
For your Reference, you can read android documentation : Click Here
You can also use android:noHistory="true" for the first activity in your AndroidManifest.xml. No need for intent flags.
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
i = new Intent(HomeActivity.this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
That should do it.
You could just call finish() in your first Activity after your call to startActivity(...).

Android: Clear the back stack

In Android I have some activities, let's say A, B, C.
In A, I use this code to open B:
Intent intent = new Intent(this, B.class);
startActivity(intent);
In B, I use this code to open C:
Intent intent = new Intent(this, C.class);
startActivity(intent);
When the user taps a button in C, I want to go back to A and clear the back stack (close both B and C). So when the user use the back button B and C will not show up, I've been trying the following:
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
But B and C are still showing up if I use the back button when I'm back in activity A. How can I avoid this?
Try adding FLAG_ACTIVITY_NEW_TASK as described in the docs for FLAG_ACTIVITY_CLEAR_TOP:
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 A would be:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
CurrentActivity.this.finish(); // if the activity running has it's own context
// view.getContext().finish() for fragments etc.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
What about adding in manifests file for related activity :
android:noHistory="true"
to the activity definition of B and C ? They will not be added to the backstack. Not sure if that is what you want.
This bothers me for a long time .Finally I worked it out by doing this:
In fragment,use:
Intent intent = new Intent(view.getContext(), A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
In Activity,use(add one more intent flag Intent.FLAG_ACTIVITY_CLEAR_TASK compared to fragment):
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Starting in API 16 (Jelly Bean), you can just call finishAffinity().
Now you can also call ActivityCompat.finishAffinity(Activity activity) with the compatibility library.
Be sure to set taskAffinity in the manifest to a package name unique to that group of activities.
See for more info:
http://developer.android.com/reference/android/support/v4/app/ActivityCompat.html#finishAffinity%28android.app.Activity%29
Add android:launchMode="singleTop" to the activity element in your manifest for Activity A
Then use intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) and
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) when starting Activity A
This means that when Activity A is launched, all tasks on top of it are cleared so that A is top. A new back stack is created with A at the root, and using singleTop ensures you only ever launch A once (since A is now on top due to ..._CLEAR_TOP).
Try using
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
and not
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
If your application has minimum sdk version 16 then you can use
finishAffinity()
Finish this activity as well as all activities immediately below it in the current task that have the same affinity.
This is work for me
In Top Payment screen remove all back-stack activits,
#Override
public void onBackPressed() {
finishAffinity();
startActivity(new Intent(PaymentDoneActivity.this,Home.class));
}
http://developer.android.com/reference/android/app/Activity.html#finishAffinity%28%29
As per Wakka in Removing an activity from the history stack...
Add android:noHistory="true" attribute to your <activity> in the AndroidManifest.xml like this:
<activity android:name=".MyActivity"
android:noHistory="true">
</activity>
For future research, try this code.
Intent intent = new Intent(context, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Use finishAffinity(); in task C when starting task A to clear backstack activities.
Use finishAffinity() to clear all backstack with existing one.
Suppose, Activities A, B and C are in stack, and finishAffinity(); is called in Activity C,
- Activity B will be finished / removing from stack.
- Activity A will be finished / removing from stack.
- Activity C will finished / removing from stack.
Use this code for starting a new Activity and close or destroy all other activity stack or back stack.
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
I found an interesting solution which might help. I did this in my onBackPressed() method.
finishAffinity();
finish();
FinishAffinity removes the connection of the existing activity to its stack. And then finish helps you exit that activity. Which will eventually exit the application.
In kotlin it is almost same like java. Only | symbol is replaced by or text.
So, it is written like-
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
I found the answers here a little misleading because the code in the original question seems to work fine for me?
With A being the root activity, starting it from B or C only with FLAG_ACTIVITY_CLEAR_TOP does remove B and C from the back stack.
It sounds to me like you need to start Activity C from Activity B by using startActivityForResult(). When you click a button in Activity C, call setResult(RESULT_OK) and finish() so Activity C is ended. In Activity B, you could have the onActivityResult() respond by also calling finish() on itself, and you'd then be taken back to Activity A.
Advanced, Reuseable Kotlin:
You can set the flag directly using setter method. In Kotlin or is the replacement for the Java bitwise or |.
intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
If use this more than once, create an Intent extension function
fun Intent.clearStack() {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
You can then directly call this function before starting the intent
intent.clearStack()
If you need the option to add additional flags in other situations, add an optional param to the extension function.
fun Intent.clearStack(additionalFlags: Int = 0) {
flags = additionalFlags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
I tried all solutions and none worked individually for me.
My Solution is :
Declare Activity A as SingleTop by using [android:launchMode="singleTop"] in Android manifest.
Now add the following flags while launching A from anywhere. It will clear the stack.
Intent in = new Intent(mContext, A.class);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(in);
finish();
Intent.FLAG_ACTIVITY_CLEAR_TOP will not work in this case. Please use (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
For more detail please check out this documentation.
Kotlin example:
val intent = Intent(this#LoginActivity, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
finish()
logout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
logout.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
for me adding Intent.FLAG_ACTIVITY_CLEAR_TASK solved the problem
Intent i = new Intent(SettingsActivity.this, StartPage.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
You can use this example to call your Activity A from Activity C
Intent loout = new Intent(context, LoginActivity.class);
loout.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(loout);
you wont get any activity pressing back button after that :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finishAffinity();
finish();
In addition to FLAG_ACTIVITY_CLEAR_TOP, you may try adding Intent.FLAG_ACTIVITY_SINGLE_TOP as well:
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
The given code works correctly. I have tried in the Application Life Cycle sample.
I haven't got B and C in the back stack after starting activity A with flag, FLAG_ACTIVITY_CLEAR_TOP
i called activity_name.this.finish() after starting new intent and it worked for me.
I tried "FLAG_ACTIVITY_CLEAR_TOP" and "FLAG_ACTIVITY_NEW_TASK"
But it won't work for me... I am not suggesting this solution for use but if setting flag won't work for you than you can try this..But still i recommend don't use it
Add NO History Flag in the intent.
In activity B, start the activity C as below >>>>>>
Intent intent = new Intent(this, C.class);
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();

How to bring an activity to front in android?

I am using moveTasktoBack() function to send my activity to background.
I want to bring my activity to front when a timer in my activity finishes. I am using the phone back key to send the activity to back. How do I do that? Help please.
Exact Same issue that is mentioned on this Question.
Sloved it with following code snippet. i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); actauul which brings back the activity to front.
Intent i=new Intent(ApplicationStatus.this,NotifyActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
i.putExtra("ID_TimeLeft",String.valueOf(TimeLeft));
startActivity(i);
I think it should be FLAG_ACTIVITY_SINGLE_TOP.
You can use intent with the appropriate flags. FLAG_ACTIVITY_NEW_TASK to create a new task to contain your activity if one doesn't already exist. FLAG_ACTIVITY_REORDER_TO_FRONT to bring your activity to the front of the task if it's not already there.
Intent intent = new Intent(context, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Categories

Resources