Android的明确总是返回堆栈 [英] Android clear ALWAYS the Back Stack

查看:281
本文介绍了Android的明确总是返回堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多的问题在这里关于这个话题,但没有一个答案对我的作品。我编程的应用程序,将随身携带的敏感数据,并为这样的原因,我希望使用户每次进入应用程序时登录。我已经试过singletask发射方式和clearTaskOnLaunch,也是我隐藏最近应用程序的任务从最近用过的属性排除。

但是,现在的应用程序不表现为它应该与后退按钮。

例如:

LoginActivity> MainActivity> HelpActivity

现在用户presses的的主页按钮的,因为应用程序是不是在近期]列出用户将需要使用的启动器图标。 LoginActivity弹出如预期,但是,如果用户presses的的后退按钮的它去Helpactivity而不是exitingn的应用程式。

编辑:我已经找到了解决办法,问题是,我是叫完成() LoginActivity 启动后 MainActivity 。因此,在发射的Andr​​oid系统被重新亲子活动将点击,当新实例 LoginActivity 在堆栈的顶部,而不是清除它只留根活动始于有没有这样的活动。


解决方案

 你试过在清单noHistory选项?
您也可以通过使用共享preferences的实现这个:
公共无效的onPause(){
    super.onPause();
        上下文的背景下= getApplicationContext();
                ActivityManager AM =(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
                清单< RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                如果(!taskInfo.isEmpty()){
                  组件名topActivity = taskInfo.get(0).topActivity;
                  如果(!topActivity.getPackageName()。等于(context.getPackageName())){                    Toast.makeText(xYourClassNamex.this,你离开你的应用程序。Toast.LENGTH_SHORT).show();
//在这里你可以保持在共享preferences说明每次你离开一个布尔值,并登录到应用程序。
                  }
                }
      }

下面是链接:<一个href=\"http://stackoverflow.com/questions/19155844/click-on-home-key-and-application-run-in-background\">click在后台主页键和应用程序运行。

There are many question here about this topic, but none of the answers works for me. I am programming an app that will carry sensitive data, and for such reason I want to make the user login each time it enters the app. I already tried the singletask launch mode and clearTaskOnLaunch, also I hide the task from the "Recents Apps" with the exclude from recents attribute.

But now the app don't behaves as it should with the Back button.

Example:

LoginActivity > MainActivity > HelpActivity

Now the user presses the Home Button, since the app is not on the recents list the user will need to use the launcher icon. LoginActivity pops up as expected but, if the user presses the Back Button it goes to Helpactivity instead of exitingn the app.

Edit: I already found the solution, the problem was that I was calling finish() on the LoginActivity after starting MainActivity. So when clicking on the launcher the Android system was re-parenting the Activities putting the new instantiated LoginActivity at the top of the stack, instead of clearing it leaving only the root Activity since there was not such Activity.

解决方案

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. 


                  }
                }
      }

Here is the link : click on home key and application run in background

这篇关于Android的明确总是返回堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆