How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear the Activity Stack?
@bitestar has the correct solution, but there is one more step:
It was hidden away in the docs, however you must change the launchMode
of the Activity
to anything other than standard
. Otherwise it will be destroyed and recreated instead of being reset to the top.
Intent.FLAG_ACTIVITY_CLEAR_TOP doesn't deletes the activity stack
Use this--
Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
Edited--New Answer and would work perfectly..
just taking an example...do it accordingly what your project needs--
I am taking three Activity
class A, B and C..and I have applied a close button on the view of class C Activity. If you want then by the Back button you can go to the previous Activity and when you press the close button then you would exit from apps..have a look--
public class AActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this, B.class);
startActivityForResult(i, 1);
}
@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == 5) {
finish();
}
}
}
Take next class activity--
public class B extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
Intent i = new Intent(this, C.class);
startActivityForResult(i, 2);
}
@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == 5) {
setResult(5);
finish();
}
}
}
Take last activity--
public class C extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
}
// close button..set by xml view..you can set it by button listener.
public void close(View v) {
setResult(5);
finish();
}
}
Hopefully, it would solve your problem..cheers!
Activity Do not clear from back stack even From Method Intent.FLAG_ACTIVITY_CLEAR_TOP
Sometimes it doesn't work you should add this line in your code
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
It will help you and hopefully your issue will resolve.
I've added this line in your code and after adding that line your code will look like this
Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
progressDialog.dismiss();
Replace your code with the above code.
How To Clear Activity Stack Below An Activity
Oh guys, I figured out that you just have to put the following code with the Intent which starts the stack clearing activity:
Intent i = new Intent(this,MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Thanks for your help though.
Clear ALL activities in back stack
Well, i ended up using this answer
Clear the entire history stack and start a new activity on Android
And that do exactly what Im trying to do
Related Topics
Can't Run Program with Processbuilder, Runs Fine from Command Line
Java Error When Trying to Run Netlogo Headlessly on a Cluster
Running a Scheduled Task Written in Java on a Linux Server
Invoking Bash from Java Interactively
Shuffle Multiple Files in Same Order
Eclipse 3.6 Helios for Ubuntu 10.10
How to Create a Linux Cluster for Running Physics Simulations in Java
Connect as User with No Password Set on Postgresql 8.4 via Jdbc
Could Not Create the Java Virtual MAChine
From What Linux Kernel/Libc Version Is Java Runtime.Exec() Safe with Regards to Memory
Split String With Dot as Delimiter
Is Multi-Thread Output from System.Out.Println Interleaved
Calling a Servlet from Jsp File on Page Load
MySQLsyntaxerrorexception Near "" When Trying to Execute Preparedstatement
Illegalargumentexception or Nullpointerexception For a Null Parameter
How to Check the Jdk Version Used to Compile a .Class File
Find Oracle Jdbc Driver in Maven Repository
Find Duplicate Value in Array List and Print the Duplicated Value