Android application Exit for home screen

≯℡__Kan透↙ 提交于 2019-12-01 11:23:04

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.

    Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);

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.

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
    }

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.

In order to go back to the Home screen, you can use the following code:

// This you have to insert inside the exit button click event
    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    onQuitPressed(); 

//This is the onQuitPressed method

//to remove application from task manager
    public void onQuitPressed() {

        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);
    }

Hope this helps!

System.exit(0) should work but don't know why its not working with you,alternate solution is to call Home 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.

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

There is a hack for this solution.

Start each Activity with the method startActivityForResult(intent, 1) and override a method onActivityResult() and in onActivityResult() put a code given below

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_CANCELED){
        setResult(Activity.RESULT_CANCELED);
        onBackPressed();
    }
}

and set a result on each activity onCreate()

setResult(Activity.RESULT_OK);

Now what you have to do is the Activity from which you want to exit your application do set

setResult(Activity.RESULT_CANCELED);

and call onBackpress method

onBackPressed();

Hope so this will help you and resolve your problem.

In all cases, use 2nd option i.e finish() to kill present activity and start another acitvity i.e StartActivity(new intent(this, whichActicityUwant.class)); then call finish();

3rd option used for killing & forcing close of the app which goes to home screen.

I know I'm late but this is how I have achieved it: use this in your fragment where you want to go to other fragment /activity.

@Override
    public void onDetach() {
        super.onDetach();
        YourFragment pass = new YourFragment();
        getActivity().getSupportFragmentManager().beginTransaction()
                .replace(R.id.container_body, pass)
                .addToBackStack("Your Fragment")
                .commit();
    }

now use this method where you are right now after using the above method:

@Override
    public void onResume() {
        super.onResume();
        if(getView() == null){
            return;
        }
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
                    Toast.makeText(getActivity(), "Exiting", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!