【问题标题】:Android clear ALWAYS the Back StackAndroid 总是清除 Back Stack
【发布时间】:2014-07-21 11:57:00
【问题描述】:

这里有很多关于这个主题的问题,但没有一个答案对我有用。我正在编写一个将携带敏感数据的应用程序,因此我想让用户每次进入应用程序时都登录。我已经尝试过单任务启动模式和 clearTaskOnLaunch,我还使用 excludes from recents 属性隐藏了“Recents Apps”中的任务。

但现在应用程序的行为与使用“后退”按钮时不同。

例子:

LoginActivity > MainActivity > HelpActivity

现在用户按下主页按钮,因为应用不在最近列表中,用户需要使用启动器图标。 LoginActivity 会按预期弹出,但如果用户按下返回按钮,它会转到 Helpactivity 而不是退出应用程序。

编辑:我已经找到了解决方案,问题是我在启动MainActivity 后在LoginActivity 上调用finish()。因此,当单击启动器时,Android 系统重新设置了 Activity 的父级,将新实例化的 LoginActivity 放在堆栈的顶部,而不是清除它,只留下根 Activity,因为没有这样的 Activity。

【问题讨论】:

  • 请发布您的清单

标签: android back-stack


【解决方案1】:
Have you tried noHistory option in manifest?
Also you can implement this by the use of SharedPreferences:
public void onPause(){
    super.onPause();
        Context context = getApplicationContext();
                ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                if (!taskInfo.isEmpty()) {
                  ComponentName topActivity = taskInfo.get(0).topActivity; 
                  if (!topActivity.getPackageName().equals(context.getPackageName())) {

                    Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP. ", Toast.LENGTH_SHORT).show();
//Here you can maintain a boolean value in sharedpreferences stating everytime you leave and login into the app. 


                  }
                }
      }

这是链接:click on home key and application run in background

【讨论】:

  • 好的,这样我就可以知道用户是否离开了我的应用程序,这将是清除堆栈的适当时机,但是该怎么做呢?在更高的 API 上我可以使用 finishAffinity(),但我的 minSdk 是 8。为什么我想在 sharedprefs 中存储一个布尔值?没看懂
  • 无论何时,如果您在任何活动中按下主页按钮,您可以将该值设置为 true,稍后通过检查该标志,您可以决定是否再次登录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 2011-05-10
相关资源
最近更新 更多