How to Exit an App to Android Home Screen on Button Pressed

The natural way to exit android app to device home screen is by pressing the device home screen button.

Although there are divided opinions on  exiting android app to home screen when a button is pressed inside an app.

That is not how android was designed or intended to work.

But there are situations we some might consider exiting an app to home screen within the app is necessary.

Below is a code snippet on how to exit android app to home screen when a button is click inside the app.

activity_home_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="24dp"
    tools:context=".Button.HomeScreenActivity">
    
    <android.support.v7.widget.AppCompatButton
        android:id="@+id/home_screen_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:text="Go To Home Screen"
        android:background="@color/colorAccent"/>

</android.support.v7.widget.LinearLayoutCompat>

We are going to use two different scenarios.

Button Click Within App

We will get the instance of the Button View and attach an event listener to it.

public class HomeScreenActivity extends AppCompatActivity {

    private static final String TAG = HomeScreenActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);

        AppCompatButton appCompatButton = (AppCompatButton)findViewById(R.id.home_screen_button);
        appCompatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent homeScreenIntent = new Intent(Intent.ACTION_MAIN);
                homeScreenIntent.addCategory(Intent.CATEGORY_HOME);
                homeScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(homeScreenIntent);
            }
        });
    }
}

Using onBackPressed() Method

We can also override Activity class onBackPressed()method and add similar code as above to it

public class HomeScreenActivity extends AppCompatActivity {

    private static final String TAG = HomeScreenActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
    }


    @Override
    public void onBackPressed() {
        Intent homeScreenIntent = new Intent(Intent.ACTION_MAIN);
        homeScreenIntent.addCategory(Intent.CATEGORY_HOME);
        homeScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(homeScreenIntent);
    }
}

In the next code snippet, you will learn different ways you can use to increase the size of android floating action button in android.

 

 

Add a Comment