1

I had wandered in the web for an hour and hour, I couldn't able find a code that closes my app completely and closes all activities then return to home screen.Even though "EXIT' button is indispensable for an android app I couldn't find a perfect code for my job.I giving the codes didn't work.So please don't suggest that.

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

this code closes app and shows homescreen.But it didn't close running app.It simply minimises the app

Activity.finish();
System.exit(0);    

    protected void quit(){
    Process.killProcess(Process.myPid());
}

This code simply restarts the current activity

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Have a look at the other answers in the linked "duplicate" question. There are many ways to do what you want, even though it is not usually done in Android. The easiest way is to launch your "root activity" using `Intent.FLAG_ACTIVITY_CLEAR_TOP` and pass an "extra" telling the root `Activity` that you want to exit. This will clear all activities from the activity stack, and your root activity just needs to call `finish()` to exit. – David Wasser Aug 15 '17 at 17:10

2 Answers2

0

This might work:

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);

Or call moveTaskToBack(true) on your Activity instead of System.exit(). This will hide your application until the user wants to use it again.

Remember, as long as you're responding to lifecycle events appropriately, neither you nor the user needs to care if your application is still running or not.

So if you want to hide your application call moveTaskToBack() and let Android decide when to kill it.

Stas Lelyuk
  • 538
  • 6
  • 19
0

Personnaly, I like to use finishAndRemoveTask(); on my MainActivity, which removes the app from the recent apps and closes it completly. Or you can simply use finishTask(); if you want your app to be on the recent apps screen.

Gueg
  • 127
  • 1
  • 11