开发者

Logout from my application

开发者 https://www.devze.com 2023-03-27 23:01 出处:网络
I have designed an app which passes through many activities开发者_StackOverflow.In every page I have set Logout button. When Logout is clicked, I finished the current Activity and use FLAG_ACTIVITY_CL

I have designed an app which passes through many activities开发者_StackOverflow.In every page I have set Logout button. When Logout is clicked, I finished the current Activity and use FLAG_ACTIVITY_CLEAR_TOP and start the Login Activity. It works but when I press the back button it loads the last activity from which I logged out.

In some suggestions I found that every activity must be finished and start the next activity. But I cant do that because when I press the back button the activity has to reload but it has to get some data from previous activity which is already finished.

Actually what I want to do is, I want to log out of my app from any page. Please any suggestions.


I managed to achieve this by also adding FLAG_ACTIVITY_NEW_TASK as well, and in my AndroidManifest, the Activity that I was launching has android:launchMode="singleTop" as well. Not sure if it's the best approach, but it works.


maybe you can finish() your activity and start the next one.

For the back button you can so use startActivityOnresult()

and so you should log out from anywhere in your app.


You may also want to add a snippet of code (or a call to a utility method) that checks if the user is logged in (and forwards them to the login activity if not) in the onResume of each of your activities. This would make sure that none of the activities could be used without being logged in - and also helps with the back button issue.


Create a subclass of Application that all your Activities fall under. Within it, maintain a static collection of Activities that need to be finished when the user logs out. Add each Activity that needs to be finished to the collection within its onCreate method. When the user logs out, iterate over the collection and call finish() on each Activity in it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号