简体   繁体   English

重置活动堆栈而不启动新活动

[英]Reset activity stack without starting new activity

I have a broadcast reciever triggered by an alarm event, which checks if the application is being idle (user is not active). 我有一个由警报事件触发的广播接收器,它检查应用程序是否处于空闲状态(用户未处于活动状态)。 Now I would like to reset the activity stack and bring the application to the first / default activity. 现在我想重置活动堆栈并将应用程序带到第一个/默认活动。

But If I do something like this: 但如果我做这样的事情:

Intent intent = new Intent(context, StartUp.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP 
                | Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

then my application comes to the foreground. 然后我的应用程序来到前台。

I would like to do it silently so the user will not be disrupted by my application (as aparently he is doing something else). 我想默默地这样做,这样用户就不会被我的应用程序打乱(显然他正在做其他事情)。

So my question is how do I clear the activity stack without launching an activity? 所以我的问题是如何在不启动活动的情况下清除活动堆栈?

Checking whether any of your activities is on the foreground seems to be covered over here , though I have not tried it. 检查你的任何活动是否是在前景似乎覆盖了这里 ,虽然我还没有尝试过。 Then if you want to terminate the whole application just do: 然后,如果你想终止整个应用程序,只需:

System.runFinalizersOnExit(true);
System.exit(0);

And you are done. 你完成了。 Hope this will help you. 希望这会帮助你。

On top of my head, why not save the state to for example the shared preferences and when user launches your application again you can detect it needs to be cleared and relaunch the activity. 最重要的是,为什么不将状态保存到例如共享首选项,当用户再次启动应用程序时,您可以检测到需要清除它并重新启动活动。 Ie use a concept of delaying the reset of the stack. 即使用延迟堆栈重置的概念。 There are probably other implications such as SharedPreferences in BroadcastReceiver seems to not update? 可能还有其他一些含义,例如BroadcastReceiver中的SharedPreferences似乎没有更新? , just to give an idea. ,只是想一个主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM