简体   繁体   中英

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).
  • I then finish() my Activity that launched that application (there are other Activities from my app still in the Activity stack).
  • If the user presses Back, then it just exits that app instead of returning to my app.

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?

If I don't finish() my Activity, then the Back button returns to my Activity, so I know that basic function is working.

Detail

My Activity stack is:

MainActivity -> LoginActivity -> ActivityA -> ActivityB

At this point, I believe adb shell dumpsys activity confirms the stack:

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

Now, my ActivityB launches an external PDF viewer application.

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.

To do this, when the app moves to the background, I broadcast an Intent, which tells ActivityA and ActivityB to finish().

When the user presses Back in the PDF viewer, I would expect them to get returned to the LoginActivity. Unfortunately, the app just seems to exit.

After the PDF viewer has launched, adb shell dumpsys activity shows the stack is:

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

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...

I believe Android reserves the right to destroy any Activity that is no longer in the foreground, so is the behaviour here just undefined?

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.

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.

When you finish() your activity, its removed from the stack.

You can't go back to it unless its created again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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