I have develop android application,there I have Exit button in menu option.I want exit application and go to home screen when I press exit button. for that I have used
following methods
1.System.exit(0)
2.finish()
3.android.os.Process.killProcess(android.os.Process.myPid())
4.System.runFinalizersOnExit(true);
these all methods direct to application my first page not to home screen.so what should I do for this?? please help me
Firstly close in an android application is frowned upon because the back button and home button are already kinda giving you this functionality.
But if you need to you can do this
When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts.
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code clears all the activities except for first activity. Then put this code inside the first activity's onCreate()
, to signal when it should self destruct when the 'Exit' message is passed.
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
1) "exit" buttons are evil. There is no need for them in Android. The user presses back or home. Let Android do the navigation.
2) never use System.exit() unless you have a very good reason and you know what you are doing. Let Android do it's own application management.
3) just use finish(). It's the documented, recommended way of closing your app.
In order to go back to the Home screen, you can use the following code:
// This you have to insert inside the exit button click event
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
onQuitPressed();
//This is the onQuitPressed method
//to remove application from task manager
public void onQuitPressed() {
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
}
Hope this helps!
System.exit(0)
should work but don't know why its not working with you,alternate solution is to call Home Intent
,
It will redirect to you at home screen but it keep your app in background, so next time when you start you app it will start from where you left last.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
There is a hack for this solution.
Start each Activity with the method startActivityForResult(intent, 1)
and override a method onActivityResult()
and in onActivityResult() put a code given below
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_CANCELED){
setResult(Activity.RESULT_CANCELED);
onBackPressed();
}
}
and set a result on each activity onCreate()
setResult(Activity.RESULT_OK);
Now what you have to do is the Activity from which you want to exit your application do set
setResult(Activity.RESULT_CANCELED);
and call onBackpress method
onBackPressed();
Hope so this will help you and resolve your problem.
In all cases, use 2nd option i.e finish()
to kill present activity and start another acitvity i.e StartActivity(new intent(this, whichActicityUwant.class)); then call finish();
3rd option used for killing & forcing close of the app which goes to home screen.