Reputation: 13
I would like to understand better what exactly is Happening when the Back-button is pressed and the MainActivity is destroyed in Terms of static variables and registered services.
this is what i know already:
What I Need to know is:
1.) what happens with static variables which are declared and initialized globally.
i.e.
static boolean logging_on_flag = false;
this is turned to true when the app is running, but if the activity is detroyed by the back-button and then recreated, will it be true or false?
2.) What happens with registered Services (listeners)?
i.e., I use Location Services to log my Location data. After registrating the Location listener, the System calls the method onLocationChanged() every time the Location changes. In that method, i do the logging. When i leave the app with the back-button, will this process be interrupted? will the listener be unregistrated?
I am confused and forced to ask the experts here, since the back-button-behaviour of my app changed since I started to work with Fragments.
Before:
leaving the app with the back-button did not Interrupt the logging process, but the non-static variables were reinstanciated when the activity was selected again. Since the listener was bound to the old variable instances, changing the new instances did not have any effect on the logging process anymore. Furthermore, the listener would be registrated again because this is Happening in onCreate(), which lead to dual logging (every measurement was logged twice).
I could solve this by making the logging_flag static (which prevented instanciating) and checking in onCreate() if the flag is already true and if this is the case, the listener was not registrated again. This worked like a charm, since the flag remained true after recreating.
After implementing Fragments here and there:
The behaviour on leaving the app by pressing the back-button seems to have changed completely. Now, the logging is stopped immediately as the back-button is pressed and when you get back to the app, it is completely restarted and even the static variable was not true anymore but false.
Can anybody explain me, why the behaviour has changed in Terms of the static variables and the services?
Please note that this is a General question to get a better understanding of the back-button. I am not asking for a solution (and yes, I know I should better work with savedInstanceState).
Upvotes: 1
Views: 257
Reputation: 3869
what happens with static variables which are declared and initialised globally.
They live as long as your the app process is living. When you go back to the home screen of the system, the app process is not necessary killed (it's managed automatically by the system if it considers it has enough resources or not). So potentially your static variable can still have the value you've set, the next time you launch the app.
What happens with registered Services (listeners)?
Depending on how you've started the service, it can still run in the background even if you don't need it!. So don't forget to stop everything when the app is stopped.
Concerning the listeners, they will still be affected to the service so it can create memory leaks if you've forgotten to un-register them.
You should avoid to use static variable to store data (you can use final static as constants).
You should use onPause
to stop services / unregister listeners... and onResume
to restart them.
Upvotes: 0
Reputation: 1632
statics are not part of the object so they still alive until you assign null to them. Or kill application.
Answering your question: 1) static will contains last value 2) Listener will be working until you kill app. You must unregister listeners on close.
Why behavior change? It didn't change. I think you have made somewhere mistake, but can't tell without seeing code.
Upvotes: 1