Avoid splash screen activity when pressing Back button

后端 未结 4 1484
悲&欢浪女
悲&欢浪女 2020-12-24 07:55

In my application, I\'ve two activities. First is a splash screen, which simply shows the application name and few other info. Upon clicking on the splash screen activity, I

相关标签:
4条回答
  • 2020-12-24 08:28

    In addition to the above answers, you should note that:

    1: by calling the finish() method, the Splash activity will close after execution, meaning that it will not be available in the stack.

    @Override
    protected    void    onCreate(Bundle saveInstsnceState){
    super.onCreate( saveInstanceState);
    
    \\ do something here
    Intent    intentSplash    =    new Intent(SplashActivity.this,    NextActivity.class);
    StartActivity(intentSplash);
    finish ();
    }
    

    You will achieve your aim using the method above but...

    2: If you want to prevent your users from force exiting the app (pressing back button) while the splash activity in still ongoing which is the best practice in android, then you need to call the onBackPressed () method.

    Class NoBackSplash{
    
    @Override
    protected    void    onCreate(Bundle saveInstsnceState){
    
    super.onCreate( saveInstanceState);
    \\ do something here
    Intent    intentSplash    =    new Intent(SplashActivity.this,    NextActivity.class);
    StartActivity(intentSplash);
    finish ();
    }
    
    @Override
    public  void  OnBackPressed(){
    
    };
    
    }
    

    With this OnBackPressed() method, your splash activity will not be force to exit no matter how hard the user try.

    0 讨论(0)
  • 2020-12-24 08:34

    In your AndroidManifest.xml file, add android:noHistory="true" attribute in your splash screen <activity>.

    0 讨论(0)
  • 2020-12-24 08:44

    As I understand, you want the splash activity to not show after changing activity. You should note activities save On Stack and with starting new activity push on it and with finish you pop on top stack. I think that if you the call finish() method your problem fix as in your splash screen activity where you call StartActivity insert finish() after

    public void onClick(View v) {
        Intent intent = new Intent(Main.this, Splash.class);
        startActivity(intent);
        finish();
    }
    

    Hope to be useful :)

    0 讨论(0)
  • 2020-12-24 08:44

    You can just call

    finish();
    

    In your Splash screen when you jump to the second screen.

    0 讨论(0)
提交回复
热议问题