简体   繁体   English

Android - 按下主页按钮后,Activity会从处理程序开始

[英]Android - Activity is starting from handler after sometime of pressing home button

I have some activities(Say A & B), And from one activity I am calling another activity by using Handler-post Delayed method. 我有一些活动(说A和B),并且从一个活动我通过使用Handler-post Delayed方法调用另一个活动。

My logic is in App starts with Activity A,and after 3 seconds goes to Activity B. & After 3 seconds, it is working perfectly. 我的逻辑是在应用程序中以活动A开始,3秒后进入活动B.&3秒后,它完美地运行。

The problem is I have set a time delay of three seconds.And During these three seconds , If I click home button , It goes background and immediately after the specified three seconds of time the application is coming back to the foreground and showing the Next Activity. 问题是我设置了三秒的时间延迟。在这三秒钟内 ,如果我单击主页按钮,它会显示背景,并在指定的三秒钟后立即返回到前台并显示下一个活动。

       try {

        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub


                finish();
                Intent i = new Intent(Asplash.this, Example.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(i);
            }
        }, 3000);

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

}




   @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
       handler.removeCallbacksAndMessages(null);
       finish();

    return super.onKeyDown(keyCode, event);
}

I am using code like this , and for back button during the HANDLER TIME is working fine and the application is completely going background. 我正在使用这样的代码,并且在HANDLER TIME期间的后退按钮工作正常,应用程序完全正在运行。 , But if I press HOME button, it initially goes background and after the completion of HANDLER TIME(3000), application is coming to fore ground. ,但如果我按下HOME按钮,它最初会变为背景,并且在HANDLER TIME(3000)完成后,应用程序即将出现。 I want it to be in background only after I press Home button also. 只有在按下Home按钮后我才希望它在后台。

Please suggest me> 请建议我>

You just put this in your onPause() method of your activity: 你只需将它放在你的活动的onPause()方法中:

@Override
public void onPause() {
    handler.removeCallbacksAndMessages(null);
    super.onPause();
}

So when your app goes to the background, it will remove the callbacks on the Handler. 因此,当您的应用程序转到后台时,它将删除处理程序上的回调。 This also works for the back button AND the home button. 这也适用于后退按钮和主页按钮。

You shold start handler from onResume() method like: 你从onResume()方法中推出启动处理程序,如:

@Override
public void onResume() {
    try {
         handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                finish();
                Intent i = new Intent(Asplash.this, Example.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(i);
           }
         }, 3000);

       } catch (Exception e) {
           e.printStackTrace();
       }
}

And remove handle in onPause() Like : 并删除onPause()中的句柄,如:

@Override
public void onPause() {
    handler.removeCallbacksAndMessages(null);
    super.onPause();
}

This will make sure to start other activity, even if you press home within 3 second 这将确保启动其他活动,即使您在3秒内按回家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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