简体   繁体   English

Android-单击启动图标或单击后退按钮时如何恢复上一个活动

[英]Android - How to resume the last activity when launched icon is clicked or when back button is clicked

I am working on a tracking application that use another third party application such as google map. 我正在使用其他应用程序(例如Google Map)的跟踪应用程序。

Lets assume my application has 4 activities ( act1, act2 , act3 and act4 ). 假设我的应用程序有4个活动(act1,act2,act3和act4)。

on act3 activity i am calling google map application to calculate the distance and view the routing. 在act3活动上,我正在调用google map应用程序以计算距离并查看路线。 But once i clicked on the back button of the tablet, or go back to the home page and clicked on the launched icon, the act1 is displaying instead of act3. 但是一旦我单击了平板电脑的后退按钮,或者返回到主页并单击了启动的图标,就会显示act1而不是act3。

However, my application is still running on background. 但是,我的应用程序仍在后台运行。

So my question here how can i make the application is opened the last activity which is act3 in our example, when the back button is pressed or when the launched icon is clicked. 所以我的问题是,在我的示例中,当按下后退按钮或单击启动的图标时,我该如何打开应用程序的最后一个活动,即act3。

Thanks, 谢谢,

Store the name of the running activity into shared preferences in the onPause of every activity (if you're exiting by pressing home button) or in onBackPressed (if exiting an activity by pressing back button). 将正在运行的活动的名称存储在每个活动的onPause(如果您通过按主页按钮退出)或onBackPressed(如果通过按向后按钮退出活动)中的共享首选项中。

Then create a launcher activity which reads the shared preferences and launches the specific activity. 然后创建一个启动器活动,该活动将读取共享的首选项并启动特定的活动。

Lastly remove the launcher activity from the activity stack so that the app doesn't go back to it on pressing back button. 最后,从活动堆栈中删除启动器活动,以使应用程序不会在按返回按钮时返回到该活动。

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

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