I create one application and never use finish() for each activity. If my user clicks on the logout button it goes to the previous page.
How can I close my previous activity and close the application?
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
use 'System. exit(0);' when you really want to exit the app.
You can shut down an activity by calling its finish() method. You can also shut down a separate activity that you previously started by calling finishActivity() .
This is one workaround that i have tried and it worked for me perfectly.
SOLUTION-1
this.finish();//try activityname.finish instead of this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
use this in the activity where you want to exit from your application....
================================================================================== Above code helps to resume your app where you last left off.
SOLUTION-2
If you want to exit from the application and also close all running activities you need to use.
onActivityResult()
For eg- suppose there are 3 activities A,B,C you navigate from A->B->C and at C you want to close all activities then use following sample.
Activity A
public class A extends Activity {
int Finish = 100;
Button launch_second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
launch_second = (Button) findViewById(R.id.start_second_act);
launch_second.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent second = new Intent(A.this,
B.class);
startActivityForResult(second, Finish);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 100:
this.finish();
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
Activity B
public class B extends Activity {
private Button launch_next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
launch_next = (Button) findViewById(R.id.start_third_activity);
launch_next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent third = new Intent(B.this,C.class);
startActivityForResult(third, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 100:
setResult(requestCode);
this.finish();
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Activity C
public class C extends Activity {
Button kill_app;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_main);
kill_app = (Button)findViewById(R.id.kill_app_btn);
kill_app.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
C.this.finish();
setResult(100);
}
});
}
}
SOLUTION-3
There is method available finishAffinity()
call it from any activity,all previous activities will get destroyed.
EDIT
Sam Janz answer is cleaner than this method. Use the intent flags to your advantage
When the user presses log out:
Intent intent = new Intent(this,MyHomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putBooleanExtra("finishApplication", true);
startActivity(intent);
Then in MyHomeActivity (Your start activity) in onResume:
if(getIntent().getBooleanExtra("finishApplication", false){
finish();
}
This way you don't have to have checks in all your activity's only the Home activity.
Dirtier option:
Create a static boolean variable in a singleton somewhere (probably in a class that extends application);
public static boolean loggingOut = false;
When the user presses log out set this to true and call finish on that activity.
YourApplication.loggingOut = true;
finish();
In each activity in onResume()
if(loggingOut){
finish();
}
Ensure you set this boolean back to false in your main/start up activity:
if(loggingOut){
finish();
YourApplication.loggingOut = false;
}
If you also want the back button to do it, override onBackPressed(), that would then also do
@Override
public void onBackPressed(){
YourApplication.loggingOut = true;
super.onBackPressed();
}
use finish() for each activity
1) Activity lifecycle
2) Shutting down Activity and managing Activities lifecycle
3) The same question answered in detail (2 approaches - Broadcast Receiver (+1) and Intent.FLAG_ACTIVITY_CLEAR_TOP)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With