How to clear stack back to root activity when user leaves application?

后端 未结 2 1725
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 12:40

I have an application with 2 activities, LogonAct and MainAct. LogonAct is a logon activity which I want to force the user to go through each time they return to the application

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 13:35

    The docs for android:clearTaskOnLaunch mention that this attribute applies "whenever [the Activity] is re-launched from the home screen".

    However, in your case you're pressing the Home button to return to the Home screen, rather than pressing the Back button. This means your application isn't actually relaunched because the MainAct was not "finished". That only happens when you press Back (or if Android kills the task to save resources etc.).

    As you only have two activities in your application, you could set the android:noHistory attribute on MainAct, thus ensuring that users can never return to it and must pass through the LogonAct.

    As an aside, it seems a bit annoying to force users to re-login every time they navigate away from the app (for example when they receive a phone call).
    You could retain a session token with timeout in your app's persistent storage, or hold a network connection open using a service if that's how your app works — but of course that's up to you and your requirements. :)

提交回复
热议问题