无法清除以前的所有活动并跳出应用程序

时间:2012-06-25 08:35:19

标签: android android-activity activity-finish

我致电

Intent intent = new Intent(activity, Main_AllLatestNews.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(intent);

但是,当我点击Main_AllLatestNews课程中的后退按钮时,它应该会退出应用,而不是回到之前的活动。

3 个答案:

答案 0 :(得分:1)

Backpressed destroy the current activity 

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // do something on back.
            this.finish();
            Intent intent = new Intent(activity, Main_AllLatestNews.class);

            startActivity(intent);
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

答案 1 :(得分:1)

试试这个:

Intent intent = new Intent(activity, Main_AllLatestNews.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(intent);
activity.finish();

答案 2 :(得分:0)

在活动D中使用代码如下所示

 public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        this.finish();
        Intent i = new Intent(Dactivity.this, AActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}