如果在Android中关闭启动活动,如何关闭应用程序

时间:2014-01-01 11:50:51

标签: android

我有一个在打开主要活动之前显示2秒的启动活动。 如果用户在显示启动活动时按下后退按钮,则会关闭启动活动。但不久之后,主要活动[由启动活动触发]开启了。

我不希望这种情况发生。如果在显示启动画面时按下后退按钮,我希望关闭整个应用程序。 我该如何做到这一点?

编辑: 下面是我的启动活动代码:

    public class Splash2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // fading transition between activities
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);

        setContentView(R.layout.activity_splash2);
        Thread timer = new Thread() {
            public void run() {
                try {
                    sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    Intent open = new Intent(
                            "com.example.puzzletimer.HOMESCREEN");
                    startActivity(open);
                }
            }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}

3 个答案:

答案 0 :(得分:2)

只需将此代码添加到您的SplashActivity中......

@Override
public void onBackPressed() {
    super.onBackPressed();
    android.os.Process.killProcess(android.os.Process.myPid());
}

或维护一个标志以确定是否在Thread中启动Activity ...

public class Splash2 extends Activity {

    private volatile boolean interrupt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // fading transition between activities
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);

        setContentView(R.layout.activity_splash2);
        Thread timer = new Thread() {
            public void run() {
                try {
                    sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if (!interrupt) {
                        Intent open = new Intent(
                                "com.example.puzzletimer.HOMESCREEN");
                        startActivity(open);
                    }
                }
            }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        interrupt = true;
    }
}

答案 1 :(得分:0)

试试这个 -

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}

答案 2 :(得分:0)

另一种方法是使用runnable:

  1. 在您的Splash2活动中声明处理程序和Runnable:

    private Handler handler;
    private Runnable startMain;
    
  2. 在onCreate()中,初始化它们并将runnable设置为在1.5秒内触发:

    handler = new Handler();
    startMain = new Runnable() {
        @Override
        public void run() {
            Intent open = new Intent("com.example.puzzletimer.HOMESCREEN");
            startActivity(open);
        }
    };      
    
    handler.postDelayed(startMain, 1500);
    
  3. 覆盖onBackPressed()并简单地取消runnable:

    @Override
    public void onBackPressed() {
        handler.removeCallbacks(startMain);
        super.onBackPressed();
    }
    
  4. 就是这样。如果按下后退键,它将取消runnable并且您的主屏幕将无法运行。