android - 注销并关闭 Android 中的所有 Activity

标签 android android-intent

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

如果我曾经转到 Activity3,然后从那里转到主页。 从那里我试图注销。它让我回到登录页面,但如果我按下手机的后退按钮,它会显示所有以前的 Activity 。请帮我看看我们该怎么做。

这是我试过的

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

    }
});

最佳答案

Login Activity 需要在 Manifest 文件中包含 android:launchMode="singleTop"。这是 the link用于堆栈和返回堆栈。

您还需要删除 loginPageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);,因为这将创建一个新任务并将 Login 设置为 root。

关于android - 注销并关闭 Android 中的所有 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19004088/

相关文章:

android - StartActivityForResult 未将数据返回给父 Activity

android - 我应该在哪里使用 Service 、 AsyncTask 和 Broadcast Receiver ?

android - 从 WebView 中的本地私有(private)目录加载图像

android - 如何解决 com.android.ddmlib.InstallException : INSTALL_FAILED_UPDATE_INCOMPATIBLE

java - Android 绘制路径未填满封闭区域

android - 使用 Intent 上传图像

android - 拉伸(stretch) GridView

android - 从 SherlockFragment 启动 ACTION_VIEW Activity 会导致 NullPointerException

android - Google App Invite - 主题更改

android - 在 WebView 中打开 PDF