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

Viewed 334661

I have an application where on the home page I have buttons for navigation through the application.

On that page I have a button "EXIT" which when clicked should take the user to the home screen on the phone where the application icon is.

How can I do that?

22 Answers

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

When u call finish onDestroy() of that activity will be called and it will go back to previous activity in the activity stack... So.. for exit do not call finish();

You can just add moveTaskToBack(true) in your exit button's onClickedListener to minimize the application.

Hope it helps.

100% works fine. this is code for Exit your app onClick (Method)

    Button exit = (Button)findViewById(R.id.exitbutton);

    exit.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            finish();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);
            Toast.makeText(getApplicationContext(), "Closed Completely and Safely", Toast.LENGTH_LONG).show();

        }
    });

Maybe my code can hepls (Main_Activity.java):

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.finish();
        exit(0);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        switch(keyCode)    {
            case KeyEvent.KEYCODE_BACK:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("My application").setMessage("Keep playing?").setIcon(R.drawable.icon);
                // Go to backgroung
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) { moveTaskToBack(true); }
                });
                // Exit from app calling protected void onDestroy()
                builder.setNegativeButton("CLOSE APPLICATION", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) { onDestroy(); }
                });
                // Close this dialog
                builder.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) { dialog.cancel(); }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
                return true;
        }
        return false;
    }

This is the easiest one

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

This will close the app and without destroying the methods services this task will be completed.

You can use finish(); moveTaskToBack(true); and System.exit(1); to quit your application.

public void onBackPressed() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("Exit Application?");
    alertDialogBuilder
            .setMessage("Click yes to exit!")
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            finish();
                            moveTaskToBack(true);
                            android.os.Process.killProcess(android.os.Process.myPid());
                            System.exit(1);
                        }
                    })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}
Related