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

后端 未结 20 2075
伪装坚强ぢ
伪装坚强ぢ 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:26

    Some Activities actually you don't want to open again when back button pressed such Splash Screen Activity, Welcome Screen Activity, Confirmation Windows. Actually you don't need this in activity stack. you can do this using=> open manifest.xml file and add a attribute

    android:noHistory="true"

    to these activities.

    
    
    

    OR

    Sometimes you want close the entire application in certain back button press. Here best practice is open up the home window instead of exiting application. For that you need to override onBackPressed() method. usually this method open up the top activity in the stack.

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

    OR

    In back button pressed you want to exit that activity and also you also don't want to add this in activity stack. call finish() method inside onBackPressed() method. it will not make close the entire application. it will go for the previous activity in the stack.

    @Override
    public void onBackPressed() {
      finish();
    }
    

提交回复
热议问题