简体   繁体   中英

splash screen activity appears when exiting app

Whenever I try to exit from my app by pressing back button twice, splash screen appears and it freezes until i press back button again. So I need to press back button three times to exit from my app. Please help me to exit from app with only two back button press.

The java code in project as follows:

public void onBackPressed()
{
    if (doubleBackToExitPressedOnce)
    {
        super.onBackPressed();
        MapsActivity.this.finish();
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click Back again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);
}

my splash screen code as follows:

public class SplashScreen extends AppCompatActivity {
    ImageView logoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        Animation anim1 = AnimationUtils.loadAnimation(this,R.anim.anim_down);
        logoView = findViewById(R.id.logoview);
        logoView.setAnimation(anim1);


        Handler handler = new Handler();

        handler.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
                startActivity(next_scrn);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        },2500);
    }
}

you should to finish splashActivity after start new activity, so use :

handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
            startActivity(next_scrn);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            SplashScreen.this.finish();
        }
    },2500);

Add finish(); after below code.

  Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
  startActivity(next_scrn);

You must finish() splash screen like as below than your condition on BackPress work

  Intent next_scrn = new Intent(SplashScreen.this,MapsActivity.class);
            startActivity(next_scrn);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            finish();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM