停止活动自动显示在最前面

时间:2019-03-10 11:25:10

标签: android android-activity

我有一个android应用程序。在那有启动画面和主屏幕。当我按下(启动页面)应用程序将进入后台,但过一会儿它会自动进入首页。我该如何解决?

飞溅

Intent in = new Intent(SplashActivity.this,
          HomeActivity.class);
          startActivity(in);
          overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.slide_out_to_top);
          finish();

3 个答案:

答案 0 :(得分:1)

您可以使用如下标记:

Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent); 

Intent.FLAG_ACTIVITY_CLEAR_TOP清除活动堆栈。

如果您要在一定时间段后开始活动,可以这样做:

new Timer().schedule(new TimerTask(){
        public void run() { 
         SplashActivity.this.runOnUiThread(new Runnable() {
            public void run() {
            Intent intent = new Intent(SplashActivity.this,HomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
          }
        });
       }
    }, 2000);

答案 1 :(得分:0)

我认为您想从splashScreen几秒钟后自动打开homeActivity。您可以为此使用Handler,这是代码:

   private static int SPLASH_TIME_OUT = 1500; // change the time according to your need

  new Handler().postDelayed(new Runnable() {


        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {

            // This method will be executed once the timer is over


                //open Activity when internet is connected


                    Intent intent = new Intent(Splash Activity.this, HomeActivity.class);
                    startActivity(intent);

                finish();

        }
    }, SPLASH_TIME_OUT);

答案 2 :(得分:0)

为防止这种情况,您需要在启动SplashActivity之前确定MainActivity是否已暂停。我假设您的SplashActivity在启动MainActivity之前已经进行了一些处理。如果在开始finish()之前调用了onPause(),则应该通过在onPause()中调用MainActivity来防止这种情况。