Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit from the application and show the home screen?

People also ask

How do you exit apps on Android?

Close one app: Swipe up from the bottom, hold, then let go. Swipe up on the app. Close all apps: Swipe up from the bottom, hold, then let go.

How do I close the home screen?

exit(0); Is probably what you are looking for. It will close the entire application and take you to the home Screen.

How do I exit Android studio?

The application automatically exits when you switch off the device. The Android architecture does not support exiting the app. If you want, you can forcefully exit the app, but that's not considered good practice.


Android's design does not favor exiting an application by choice, but rather manages it by the OS. You can bring up the Home application by its corresponding Intent:

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

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();
}

System.exit(0);

Is probably what you are looking for. It will close the entire application and take you to the home Screen.


This works well for me.
Close all the previous activities as follows:

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("Exit me", true);
    startActivity(intent);
    finish();

Then in MainActivity onCreate() method add this to finish the MainActivity

    setContentView(R.layout.main_layout);

    if( getIntent().getBooleanExtra("Exit me", false)){
        finish();
        return; // add this to prevent from doing unnecessary stuffs
    }

first finish your application using method finish();

and then add below lines in onDestroy for Removing Force close

android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();