简体   繁体   English

如果后台堆栈中的活动已完成,“返回”按钮是否会返回到之前的“活动”?

[英]If Activities in the back stack are finished, will Back button return to earlier Activities?

Summary 摘要

Here's my scenario: 这是我的情景:

  • My app launches an external application (a PDF viewer). 我的应用程序启动了一个外部应用程序(PDF查看器)。
  • I then finish() my Activity that launched that application (there are other Activities from my app still in the Activity stack). 然后我完成()我的Activity启动了该应用程序(我的应用程序中的其他活动仍然在Activity堆栈中)。
  • If the user presses Back, then it just exits that app instead of returning to my app. 如果用户按下Back,那么它只退出该应用程序而不是返回到我的应用程序。

Should Android work back though the Activity stack until it finds a running Activity for my app, or will it give up if it can't find the previous Activity? Android应该通过活动堆栈返回工作,直到它找到我的应用程序的运行活动,或者它是否会放弃,如果它找不到以前的活动?

If I don't finish() my Activity, then the Back button returns to my Activity, so I know that basic function is working. 如果我没有完成()我的Activity,那么Back按钮返回到我的Activity,所以我知道基本功能正在运行。

Detail 详情

My Activity stack is: 我的活动堆栈是:

MainActivity -> LoginActivity -> ActivityA -> ActivityB MainActivity - > LoginActivity - > ActivityA - > ActivityB

At this point, I believe adb shell dumpsys activity confirms the stack: 此时,我相信adb shell dumpsys activity确认了堆栈:

Hist #11: HistoryRecord{40c0c018 com.mypackage/com.mypackage.ActivityB} 历史#11:HistoryRecord {40c0c018 com.mypackage / com.mypackage.ActivityB}
Hist #10: HistoryRecord{40d27af0 com.mypackage/com.mypackage.ActivityA} 历史#10:HistoryRecord {40d27af0 com.mypackage / com.mypackage.ActivityA}
Hist #9: HistoryRecord{40d31368 com.mypackage/com.mypackage.LoginActivity} 历史#9:HistoryRecord {40d31368 com.mypackage / com.mypackage.LoginActivity}
Hist #8: HistoryRecord{40db2430 com.mypackage/com.mypackage.MainActivity} 历史#8:HistoryRecord {40db2430 com.mypackage / com.mypackage.MainActivity}
Hist #7: HistoryRecord{40c2a978 com.mypackage/com.mypackage.LoginActivity} 历史#7:HistoryRecord {40c2a978 com.mypackage / com.mypackage.LoginActivity}
Hist #6: HistoryRecord{40bf6008 com.mypackage/com.mypackage.LoginActivity} 历史#6:HistoryRecord {40bf6008 com.mypackage / com.mypackage.LoginActivity}
Hist #5: HistoryRecord{40542d58 com.htc.launcher/.Launcher} 历史#5:HistoryRecord {40542d58 com.htc.launcher / .Launcher}

Now, my ActivityB launches an external PDF viewer application. 现在,我的ActivityB启动了一个外部PDF查看器应用程序。

My app has some a specific security requirement that means I need to force a user to reenter their PIN before they can see the information in the app again. 我的应用程序有一些特定的安全要求,这意味着我需要强制用户重新输入他们的PIN,然后才能再次在应用程序中查看信息。

To do this, when the app moves to the background, I broadcast an Intent, which tells ActivityA and ActivityB to finish(). 为此,当应用程序移动到后台时,我广播一个Intent,告诉ActivityA和ActivityB完成()。

When the user presses Back in the PDF viewer, I would expect them to get returned to the LoginActivity. 当用户在PDF查看器中按Back时,我希望它们返回到LoginActivity。 Unfortunately, the app just seems to exit. 不幸的是,该应用似乎似乎退出。

After the PDF viewer has launched, adb shell dumpsys activity shows the stack is: PDF查看器启动后, adb shell dumpsys activity显示堆栈是:

Hist #7: HistoryRecord{40b97a08 com.adobe.reader/.AdobeReader} 历史#7:HistoryRecord {40b97a08 com.adobe.reader / .AdobeReader}
Hist #6: HistoryRecord{40542d58 com.htc.launcher/.Launcher} 历史#6:HistoryRecord {40542d58 com.htc.launcher / .Launcher}
Hist #5: HistoryRecord{40978318 com.google.android.gm/.ConversationListActivity} 历史#5:HistoryRecord {40978318 com.google.android.gm/.ConversationListActivity}

mLastPausedActivity: HistoryRecord{40b4aa90 com.mypackage/com.mypackage.ActivityB} mLastPausedActivity:HistoryRecord {40b4aa90 com.mypackage / com.mypackage.ActivityB}

Does anybody know how this scenario should work? 有谁知道这种情况应该如何运作? Naively, I would expect Android to work its way back through the back stack until it reaches the LoginActivity... 天真地,我希望Android能够通过后台堆栈工作,直到它到达LoginActivity ...

I believe Android reserves the right to destroy any Activity that is no longer in the foreground, so is the behaviour here just undefined? 我相信Android保留销毁任何不再在前台的Activity的权利,所以这里的行为是不确定的?

This app is very complicated, so I can't just completely throw out the existing design. 这个应用程序非常复杂,所以我不能完全抛弃现有的设计。 There are numerous entry points (widgets, notifications, Back button), and other features that depend on the UI being told that the application has been interrupted. 有许多入口点(窗口小部件,通知,后退按钮),以及依赖于被告知应用程序已被中断的UI的其他功能。

I am investigating other workarounds (eg using startActivityForResult , setting Intent.FLAG_ACTIVITY_NO_HISTORY for ActivityB, etc), but I would be interested to know how Android works back through the stack. 我正在调查其他解决方法(例如使用startActivityForResult ,为ActivityB设置Intent.FLAG_ACTIVITY_NO_HISTORY等),但我很想知道Android如何通过堆栈工作。

When you finish() your activity, its removed from the stack. 完成()您的活动后,将其从堆栈中删除。

You can't go back to it unless its created again. 除非再次创建,否则你不能再回头了。

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

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