注销并关闭Android中的所有活动

时间:2013-09-25 11:48:37

标签: android android-intent

Login > HomePage ->Activity1 ->Activity2->Activity3

如果我曾经去过Activity3,那么从那里到主页。 从那里我想尝试退出。它将我发回“登录”页面,但如果我按下手机的后退按钮,则会显示之前的所有活动。请帮助我们如何做到这一点。

这就是我试过的

logout.setOnClickListener(new OnClickListener() {    
    @Override
    public void onClick(View arg0) {
        SharedPreferences myPrefs = getSharedPreferences("SelfTrip", MODE_PRIVATE);
        SharedPreferences.Editor editor = myPrefs.edit();
        editor.clear();
        editor.commit();
        Log.d(TAG, "Now log out and start the activity login");
        Intent loginPageIntent = new Intent(getApplicationContext(), LoginPage.class);
        loginPageIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        loginPageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(loginPageIntent);

    }
});

5 个答案:

答案 0 :(得分:3)

Login活动需要在清单文件中有android:launchMode="singleTop"。这里是堆栈和后栈的the link

您还需要删除loginPageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);,因为这会创建一个新任务并以root身份登录。

答案 1 :(得分:3)

// check sharedPreferences in all activities
// when you press back button then it will close activity
@Override
protected void onResume() {
    super.onResume();
    int userId = sharedPreferences.getInt(Login.user_id, 0);

    if(userId==0){
        finish();
    }
}

答案 2 :(得分:1)

您可以简单地强制活动,不要在后台堆放历史记录。这可能会带来麻烦,但只要您的活动被称为线性活动,就应该可以正常工作。

将带有noHistory的行添加到清单:

<activity
        android:name="com.example.MainActivity"
        android:label="@string/app_name"           
        android:screenOrientation="portrait"

        android:noHistory="true" >
</activity>

答案 3 :(得分:0)

点击这里链接有一些很好的例子可以关闭所有活动。这里有示例代码下载它。More info

答案 4 :(得分:0)

您可以使用其他方式,即在活动类型的arraylist中添加活动,并从任何其他活动中完成您想要的活动。