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

后端 未结 20 2035
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 09:45

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 t

相关标签:
20条回答
  • 2020-11-22 10:04

    Here's what i did:

    SomeActivity.java

     @Override
        public void onBackPressed() {
                Intent newIntent = new Intent(this,QuitAppActivity.class);
                newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(newIntent);
                finish();
        }
    

    QuitAppActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          finish();
    }
    

    Basically what you did is cleared all activities from the stack and launch QuitAppActivity, that will finish the task.

    0 讨论(0)
  • 2020-11-22 10:06

    There is another option, to use the FinishAffinity method to close all the tasks in the stack related to the app.

    See: https://stackoverflow.com/a/27765687/1984636

    0 讨论(0)
  • 2020-11-22 10:07

    I tried exiting application using following code snippet, this it worked for me. Hope this helps you. i did small demo with 2 activities

    first activity

    public class MainActivity extends Activity implements OnClickListener{
        private Button secondActivityBtn;
        private SharedPreferences pref;
        private SharedPreferences.Editor editer;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            secondActivityBtn=(Button) findViewById(R.id.SecondActivityBtn);
            secondActivityBtn.setOnClickListener(this);
    
            pref = this.getSharedPreferences("MyPrefsFile", MODE_PRIVATE);
            editer = pref.edit();
    
            if(pref.getInt("exitApp", 0) == 1){
                editer.putInt("exitApp", 0);
                editer.commit();
                finish();
            }
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.SecondActivityBtn:
                Intent intent= new Intent(MainActivity.this, YourAnyActivity.class);
                startActivity(intent);
                break;
            default:
                break;
            }
        }
    }
    

    your any other activity

    public class YourAnyActivity extends Activity implements OnClickListener {
        private Button exitAppBtn;
        private SharedPreferences pref;
        private SharedPreferences.Editor editer;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_any);
    
            exitAppBtn = (Button) findViewById(R.id.exitAppBtn);
            exitAppBtn.setOnClickListener(this);
    
            pref = this.getSharedPreferences("MyPrefsFile", MODE_PRIVATE);
            editer = pref.edit();
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.exitAppBtn:
                Intent main_intent = new Intent(YourAnyActivity.this,
                        MainActivity.class);
                main_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(main_intent);
                editer.putInt("exitApp",1);
                editer.commit();
                break;
            default:
                break;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:08

    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);
    
    0 讨论(0)
  • 2020-11-22 10:10

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

    0 讨论(0)
  • 2020-11-22 10:13

    (I tried previous answers but they lacks in some points. For example if you don't do a return; after finishing activity, remaining activity code runs. Also you need to edit onCreate with return. If you doesn't run super.onCreate() you will get a runtime error)

    Say you have MainActivity and ChildActivity.

    Inside ChildActivity add this:

    Intent intent = new Intent(ChildActivity.this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);
    return true;
    

    Inside MainActivity's onCreate add this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        mContext = getApplicationContext();
    
        super.onCreate(savedInstanceState);
    
        if (getIntent().getBooleanExtra("EXIT", false)) {
            finish();
            return;
        }
        // your current codes
        // your current codes
    }
    
    0 讨论(0)
提交回复
热议问题