I am developing an app with four activities. When the app is opened, I am playing a background music which should continue be playing for all the activities. But when the app goes to background, the music should pause and when the app resume, the music should continue again.
Please suggest the best way to achieve this.
Thanks,
Anand.
Anand, you need to stop your music, when activity goes to background.
#Override
protected void onPause() {
super.onPause();
//stop or pause your music here.
}
and you need to play your music, when activity resumes.
#Override
protected void onResume() {
super.onResume();
// play your music here.
}
It depends on how you are playing the music. The answer is almost certainly to use the lifecycle methods. For the activity, I think the best methods to override are onResume and onPause. These methods are called when an activity appears in the foreground, and gets hidden respectively. Services have their own lifecycle, but it behaves in a different way.
Edit: Just read the question more closely. To do this between multiple activities, the music should be played via a bound service. By being selective about where the service is bound and unbound, it will stay alive between activity transitions. I think that when a new activity is created, onResume is called before on pause. So, you bind the service to the new activity in onResume, and unbind it from the old one in onPause. When the activitys are closed, the same methods are called, but on the opposite activities.
Edit 2: You could also bind the activity to the service in onCreate and onDestroy. This would cause the service to persist until all activity are removed by the OS, or the app is closed. This would mean that you would also need play and pause methods on the service, and a mechanism to ensure that only the correct activity can call them. Perhapse, when you call the playMusic function, you can provide an object to act as a token. When you call pause or stop, you provide that token again. The result will be that only the activity that called play most recently can stop the music, and all activities call play when they bind to the service, or when onResume is called.
Related
I am working on a WebRtc Calling App. When my application is in background when incoming call come the Calling Activity PopsUp for call . And When call hung Up i need to send app to the recent background state.
So i came around a solution moveTaskToBack(), which is working perfectly but the problem is after calling Activity finish it shows the Last Activity for 2 or 3 seconds and then going back to recent app. Also tried in onStop() but the result remain the same .
#Override
protected void onDestroy() {
moveTaskToBack(true);
}
Is there a better Alternative of moveTaskToBack() without any Delay? If anyone need more clarification on question let me know in comments. Thanks in advance.
You shouldn't do it onDestroy, because it's not guaranteed that to be called in Activity Lifecycle.
In the onStop() method, the app should release almost all resources that aren't needed while the user is not using it. For example, if you registered a BroadcastReceiver in onStart() to listen for changes that might affect your UI, you can unregister the broadcast receiver in onStop(), as the user can no longer see the UI. It is also important that you use onStop() to release resources that might leak memory, because it is possible for the system to kill the process hosting your activity without calling the activity's final onDestroy() callback.
Problem might be the time takes to calling onDestroy. You can try to call it where you are finishing activity.
I have an android activity with "singleTask" flag in manifest. I am closing streaming operation in onPause state of activity. Problem is, onPause() -> and onResume() is called again and again when my background service tries to reopen it. Because of that my streaming is getting disconnected.
I tried to pause streaming in onStop method, but it has some side effects, like in few cases we are able to hear sound in background of streaming.
Can you please let me know is there a way to avoid onPause->onResume call if singleTask activity is already open?
Thanks
I am porting an iOS App to Android, and am having a little difficulty with the terminally.
I want the App to sync when starting/coming to the foreground (i.e. the user selects the App) and when the App goes to the background (e.g. when going to the main screen and start using another App.)
Is there an easy way to do that in Android?
I added onResume() and onPause() to my Activities, but this will be triggered when the activity resumes or pauses, not when the App as a whole pauses (and results in a lot of sync each time the user does something).
Update:
The Sync operation is a Service which is called when the App needs to get new/updated information from the server. As such, this needs to be done when the Application is started/resumed by the suer (after inactivity for some time) and it needs to do this when the user actively stops using the Application.
when an activity goes to background,
onPause() -> onStop()
methods will be executed in sequence, if activity is partially visible, only onPause(), if activity is completely invisible by another activity, onStop() method will be executed after onPause().
onDestroy()
method will be executed if you either called finish() or when there is no enough memory in the system and system destroys it. You can't rely onDestroy() method unless you call finish() method.
It's best to use onStop() method to save your data.
nr4bt has a valid point and depending on your required functionality, a good option.
A global approach would be to start a Service when your application starts.
Make this Service responsable for your syncing operations with suitable callbacks to your active Activity.
Make this Service aware of your alive/paused/stopped Activity's. When the last one is finished -> Do your final sync and kill the Service
This question already has answers here:
Press home button to stop service
(3 answers)
Closed 6 years ago.
I have a service that I am using to play background music for multiple activities. Right now it plays through all the proper activities but I cannot get it to shut off when the home button is pressed. The game will close but the music is still playing. I have tried using the onUserLeaveHint method but that shuts the music off whenever I switch to a different activity. Not just when I leave the game.
All the activities should bind to the service. because service is alive only if one or more activities binded to it. So if no bindings to your service, then the service will be destroyed. please check this answer how to bind/unbind services. So avoid using starservice() (then you have to call stopservice() to able to distroy your service), use bindservice() to start a service - note that in this case only onCreate() will be called at your service
Have you considered using fragments? If you were to use fragments then you could stop the audio service in the onStop() method of the main activity. If you don't want to/can't use fragments for your app, you would have to stop the service from the onStop() of every one of your activities, or implement broadcast receivers in each of your activities: how-to-get-broadcastreceiver-for-action-android-intent-action-main-and-android
Just take advantage of the fact that while transitioning from an activity to another, onStart() of the second one gets called before onStop() of the other.
Given that, along with the fact that a service bound with BIND_AUTO_CREATE flag is destroyed whenever it has no bound activities (check this other answer), you can bind to the service in any your activities onStart() method and unbind in onStop().
Whenever you have an activity transition, there will be an active bound activity, but when the current activity is backgrounded (because of home or incoming call) only onStop() will be called and thus the service will be unbounded.
I am developing an app which has multiple activities. User can navigate to any activity. I start background music from first main activity and it keeps playing throughout the application. Now I want that whenever user presses HOME key, the media player should pause playing and when user comes back to app, it starts playing again. First I made media player static and was pausing music in onPause() and playing in onResume() but it creates a jerk while switching between activities. I hope you got my point. Any idea how to pause playing when HOME key pressed and play it again when user comes back?
Take a look at Activity.onUserLeaveHint()
http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
Called as part of the activity lifecycle when an activity is about to
go into the background as the result of user choice. For example, when
the user presses the Home key, onUserLeaveHint() will be called, but
when an incoming phone call causes the in-call Activity to be
automatically brought to the foreground, onUserLeaveHint() will not be
called on the activity being interrupted. In cases when it is invoked,
this method is called right before the activity's onPause() callback.
I had the same issue and solved it in an ugly way: there is a global static player. in the onPause of an activity it calls the player to stop (but it does not actually stop), and in onResume call to start.
In the player onStart I mark the music as playing. In onStop, I mark the music as stopped but not actually stop it. I wake up 1 second after stop was called , and if there was no "startPlaying" in the last second, I stop the music.
I hope there is a better way.