Reputation: 4287
Devices use to go into a "sleep" mode and it results in launching my activity's onCreate() method once again, which isn't desired.
For instance, I have an activity with VideoView which gets initialized in onCreate method, so when a device "sleeps" it recreates my player and launches player.
How to overcome this ? Is it related to system broadcasts ?
Upvotes: 0
Views: 1773
Reputation: 10063
Ultimately, you can never prevent the system from killing your application once onPause()
has been called. You must always write your application so that it can be restarted. In practice, this means a) implementing onRetainNonConfigurationInstance()
to catch very short-term destruction and re-creation of your app, b) overriding onSaveInstanceState(Bundle)
to catch medium-term destruction/re-creation, and c) overriding onPause()
to handle long-term destruction/re-creation.
In most cases, onSaveInstanceState()
is the only one you really need to implement, but onRetainNonConfigurationInstance()
can be a real optimization.
In your particular case, your onSaveInstanceState()
would write the URI of your video file and the last-played timestamp into the Bundle, so you can return to the last place played in the video when your app is re-created.
As for wakelocks, these are acceptable for e.g. keeping your device from going to sleep in the middle of watching a video. They are not acceptable for keeping your application from being killed just so you don't have to go through the trouble of going through onCreate() a second time. First, it kills the battery, and second, it's no guarantee anyway.
One final note: if you're not doing so, you should make sure your video playing activity has the "singleTop" attribute set, so you don't get multiple instances in the activity stack.
Upvotes: 1
Reputation: 4497
You could use the AlarmManager to trigger the refreshes of your widget. When scheduling your next cycle you can define whether to wake up your device (aka perform the actual task).
alarmManager.set(wakeUpType, triggerAtTime, pendingIntent);
Upvotes: 0