Reputation: 192
I am working on Android:: Media Player Application. The application has 3 activities for simplicity sake:: A, B and C are the activities.
When I am switching from Activity A to B: onPause() and onStop() of Activity A are getting called. But when I go to home screen from Activity A, the same functions: onPause() and onStop() are getting called.
Currently I am facing a problem that: If user goes to home screen:: The settings like outdoor visibility, brightness etc has to be reset to system and on the other hand, If I am switching between the activities, I have to maintain the values as per application specific.
How can I distinguish that, I am switching between the activities of the same application (or) going to home screen.
Upvotes: 0
Views: 392
Reputation: 41510
You should make these changes every time when activities come to the screen and restore the settings when they disappear from the screen. Activities are supposed to be like separate modules, so it's a good thing to make them less dependent.
This is best done with creating a template activity. Make all the necessary changes in the onResume()
and onPause()
methods and derive all other activities from this one. This way your settings management code will be called automatically in every activity.
As for the general application state, you can't really detect whether your application is present on the screen or not because an application in Android is just a bunch of independent components (activities, services, content providers and broadcast receivers). There is no single application with particular visibility states per se. Only activities have it, independently of each other.
Upvotes: 0
Reputation: 8090
Perhaps you could use SharedPreferences to store the app specific settings while your app is in use. And always restore then in onStop()
Upvotes: 0
Reputation: 72331
I think the solution is to detect when the user presses the HOME button. You can do this by overriding the method
onUserLeaveHint()
of the Activity Class.
Upvotes: 1