I\'ve got 4 activities say Act1
, Act2
, Act3
and Act4
.
A button in Act1 opens Act2, a button in Act2 opens Act3, a button in A
If you have another activity behind ( in the application activity stack),
you could use finish()
to exit the current activity.
However, the back function too suppose to remove the current activity from your application activity stack.
In my experience, it is very unreliable to rely on the activities in the back stack ( in the application activity stack). Because of that I used to exit each activity when I go deeper.
In your case:
Act 1 -> exit -> Act 2 -> exit -> Act 3 -> exit -> Act 4 -> exit -> Act 1 -> exit
The above method will exit all the activities.
However, if you want to run some code in the background, it is better to rely on a "Service" rather than an "Activity". You can let the "Service" exit after doing its assigned work.
Use android:noHistory = "true"
in your AndroidManifest.xml file
<activity
android:name=".Act1"
android:noHistory="true" />
<activity
android:name=".Act2"
android:noHistory="true" />
<activity
android:name=".Act3"
android:noHistory="true" />
<activity
android:name=".Act4"
android:noHistory="true" />
Do a
<code>
try
{
finalize();
finish
}
catch(Exception error)
{
error.printStackTrace();
}
//This would exit your App neatly
</code>
Intent intent = new Intent(Act4.this, Act1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Clear the flag before you switch back to previous activity. It might help you.
Tray this one
Intent intent =new Intent(MainActivity.this, ClassName.class);
finish
startActivity(intent);
Check out this link:
Click here
You can use :
@Override
public void onBackPressed()
{
moveTaskToBack(true);
}
in all activities to close the app.