3
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);

    }
});
4

4 回答 4

3

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

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

于 2013-09-25T11:50:58.480 回答
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();
    }
}
于 2014-01-27T06:57:11.893 回答
1

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

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

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

        android:noHistory="true" >
</activity>
于 2013-09-25T11:59:19.690 回答
0

您可以使用另一种方式,即在活动类型的数组列表中添加活动,并从任何其他活动中完成您想要的活动。

于 2013-09-25T13:29:19.723 回答