I have an application where on the home page I have buttons for navigation through the application.
On that page I have a button \"EXIT\" which when clicked should t
Add following lines after finish();
in onDestroy()
:
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
100% works fine. this is code for Exit your app onClick (Method)
Button exit = (Button)findViewById(R.id.exitbutton);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
Toast.makeText(getApplicationContext(), "Closed Completely and Safely", Toast.LENGTH_LONG).show();
}
});
May be you can try something like this
Suppose in our application, we have a number of activities(say ten) and we need to exit directly from this activity. What we can do is, create an intent and go to the root activity and set flag in the intent as
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
also, add some extra like boolean to the intent
intent.putExtra("EXIT", true);
Then in root activity, check the value of the boolean
and according to that call finish(), in the onCreate()
of the root activity
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
If you want to exit from your application. Then use this code inside your button pressed event. like:
public void onBackPressed()
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
if you want to exit application put this code under your function
public void yourFunction()
{
finishAffinity();
moveTaskToBack(true);
}
//For an instance, if you want to exit an application on double click of a
//button,then the following code can be used.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 2) {
// do something on back.
From Android 16+ you can use the following:
finishAffinity();
moveTaskToBack(true);
}
return super.onKeyDown(keyCode, event);
}
You can just add moveTaskToBack(true)
in your exit button's onClickedListener
to minimize the application.
Hope it helps.