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
In all cases, use 2nd option i.e
finish()
to kill present activity and start another acitvity i.eStartActivity(new intent(this, whichActicityUwant.class)); then call finish();
3rd option used for killing & forcing close of the app which goes to home screen.
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.
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.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.
There is a hack for this solution.
Start each Activity with the method
startActivityForResult(intent, 1)
and override a methodonActivityResult()
and in onActivityResult() put a code given belowand set a result on each activity
onCreate()
Now what you have to do is the Activity from which you want to exit your application do set
and call onBackpress method
Hope so this will help you and resolve your problem.
System.exit(0)
should work but don't know why its not working with you,alternate solution is to callHome 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.
In order to go back to the Home screen, you can use the following code:
Hope this helps!