How to detect if application was restarted?
from the CommonsWare Community archivesAt February 10, 2019, 3:21pm, hazico asked:
Hi,
When I have an Activity in foreground and I rotate the screen the Activity is recreated (and the bundle is not null)
If I go to background and then go to the system settings and remove permissions and then go back to the app, the activity is recreated (again the bundle is not null) but also the process is new, and in that case if I want to go to the main Activity because I have some objects that should hold state and now it’s null.
How can I know if the process was restarted so I go to the “main” Activity instead of the last opened Activity?
Thanks
At February 10, 2019, 3:43pm, mmurphy replied:
¯\_(ツ)_/¯
Look to see if the objects are null
, and handle that case accordingly.
Ideally, that would not be by going to another activity, but instead by handling it where you are, so the user does not “lose their place” in your app. For example, if you need the user to authenticate to set up this global data, put the authentication UI in a fragment, and show that fragment in the current activity.
At February 11, 2019, 12:25pm, hazico replied:
OK,
So let’s say there’s an object that I can check, so I can do the following test:
if (object==null && bundle!=null) {
// then the process is new and I the activity is recreated}
else if (object!=null && bundle!=null){
// activity is recreated but same process (rotation changed for example)}
Is that correct?
And where is the correct place to perform this check, because I still need to go to the first Activity and if I do it on the onCreate() then I should do it in every Activity that I have and then skip the rest of the code in onCreate and change to another Activity?
(it’s not just authentication problem, I have some api requests and check that happen in the splash screen)
At February 11, 2019, 12:45pm, mmurphy replied:
Is that correct?
IMHO, ideally, you just check if (object == null)
and proceed from there. This code should exist in one place (e.g., the repository managing access to this global data) and should be usable from everywhere, and the saved instance state Bundle
of each activity will be different.
it’s not just authentication problem, I have some api requests and check that happen in the splash screen
IMHO, that is not a good plan. Have those be able to be invoked from anywhere (e.g., via a repository).
And where is the correct place to perform this check, because I still need to go to the first Activity and if I do it on the onCreate() then I should do it in every Activity that I have and then skip the rest of the code in onCreate and change to another Activity?
IMHO, the check should be in the repository that is mediating access to this global data. Your API to that global data should be reactive (RxJava, LiveData
, coroutines, ordinary callbacks, etc.), so the repository can perform the API calls if needed.
At February 11, 2019, 8:17pm, hazico replied:
I understand that it’s better to put it in one place (the repository), but from where should I access the repository? My “starting point” when handling new instance of the application is the onCreate of the last Activity, isn’t it? is there some place that I can catch it before the onCreate?
And about the other things you suggested (not have dependency on the login and splash screens) I agree with you but at the moment it’s not really possible for me to do it because it is already written as I described and it will take a lot of time to change. so I have to compromise and at least change the Activity to “restart” the app to prevent a crash.
At February 12, 2019, 12:10am, mmurphy replied:
I can’t really answer that, sorry, as I don’t know much about your app.
If by “new instance of the application”, you mean a new process, then either you will return to the last activity in your task or to the launcher activity, depending on how long the user was gone.
There is onCreate()
of a custom Application
, though ideally you minimize the work that you do there.