On Android, it is possible for users to use the Settings -> Application Manager -> App -> Clear Data feature and clear all the stored data for the app. However, if the user uses the app switcher to switch back to the app it will switch back to the activity last used. In some cases, say with an app that requires login, the activity will be inappropriate for the case where there is no data. How should application handle his case? Should each Activity be derived from a base Activity class that checks if the data has been deleted and then launch the appropriate Activity (say, the login Activity)? Is there a more elegant solution than that?
-
on clearing data, the app will be automatically removed from the app switcher.. – nobalG Jul 22 '16 at 05:37
-
@nobalG That's not what's happening for me. If I run my app, hit the home button, the use "Clear Data" and then go to the app switcher, my app will still be there. I'm running Android 4.4.2. – ThomasW Jul 22 '16 at 05:59
-
Is it opening the same screen which is being cached in the switcher, or your app starts from the very first screen(the default screen)? – nobalG Jul 22 '16 at 06:01
-
@nobalG It is a bit strange, it actually isn't the last Activity, but the activity before the last used activity, however, neither of these activities is the default activity. – ThomasW Jul 22 '16 at 07:17
-
I just checked and this problem doesn't occur with devices running Android OS 5 or later. I've only seen this problem with Android 4.4.2 devices. – ThomasW Jul 22 '16 at 09:00
2 Answers
Instead of checking if data has been deleted, just check if the user is signed in. If he isn't, you can just send him back to the login screen as you suggested.
In any case, if you are using oAuth, or ever intend to implement it for login, checking if the user is logged in should be implemented since the oAuth tokens eventually expire. In this case, no data has been deleted, but the user is no longer signed-in, which would lead to them getting stuck in the inappropriate Activity
anyway.
Just create a super-class for your ActivityThatRequiresLogin
that will check if the user is logged in, and have all ActivityThatRequiresLogin
extend that class. Then, you can call super whenever the onCreate
and onResume
method is called.
If the user wipes their data, they are automatically signed out, so all you have to do is check if they're signed in or not.

- 106
- 2
-
Yes, in my case I can check if the user is logged in, but I'm also thinking about a more general case where the app doesn't involve logging in. – ThomasW Jul 22 '16 at 03:43
-
Hold on, I completely forgot about this, but how exactly is your app even openable? Clear data will automatically kill your app, so it should open the default Activity when you pop it back open, unless you did something weird with your Activity's onDestroy() or onStop() method. Instead of wiping the data, try force closing your app and re-opening it. It should re-open back at the launcher activity. If not, you have something specific to your code that is causing the bug. – Henry Dang Jul 22 '16 at 04:50
-
I mentioned it in my question, but if you use the app switcher to go back to your app it opens the last used Activity. When you use "Clear Data" it doesn't clear the app from the app switcher. – ThomasW Jul 22 '16 at 04:56
Pressing Clear Data stops your app's process, so your code will not be running.
Unfortunately there is no way to catch intent of such action unless you'r a system app.
The best practice way to handle this situation would use a SharedPreference mechanism and a base activity class.