I am developing an android app which has multiple activities. I have added a soundtrack also. What I want is to keep playing sound when user is switching between activities but as soon as user leaves it should stop. Help me please
To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). The system invokes each of these callbacks as an activity enters a new state. Fore more information click here
Figure 1 presents a visual representation of this paradigm.
To work some thing in the background , you need to use the Service.
for example you may refer :this sample from Google
In the above code Find How MusicService used to achieve your problem. for more search "Android service example".. hope it helps..
MusicService link
Related
I know that, unlike onCreate(), Application class does not have a onDestroy() method. But I wanted to know when my application is closed (or it is not visible on screen anymore). After all, whatsapp and many more similar chat applications can detect when user has left the app, and can record user's last online time. I want to achieve a similar thing. Also, when the application is destroyed, I want to detach all listeners attached to firebase databse.
I have already seen this question, but the accepted answer there is unreliable. So, what is the workaround for onDestroy() for me.
if you are talking about Application class (detecting when it is destroyed) - this is impossible, when Application gets killed developer shouldn't (and don't) have option for executing own code (as it may e.g. restart app from scratch)
but you are talking about app visibility, probably any Activity present on screen - extend Application class (and register it in manifest) and use ActivityLifecycleCallbacks with additional counting code: counter++ when any onActivityStarted and counter-- when onActivityStopped. also in onActivityStopped check if your counter==0, if yes then all your Activities are in background, so app isn't visible on screen (still it doesn't mean that its destroyed/killed)
edit: check out THIS example. or inspect supporting class ProcessLifecycleOwner (which probably is counting visible Activities for you and only calls onAppBackgrounded when all are gone)
You do not need onDestroy callback for it . You should be Doing it in onStop() of ProcessLifecycleOwner . Upon Application destroy your process will be destroys anyways in idle situation so no need to remove listeners there .
Remove the listeners in onStop and attach again in onStart . You can configure Application class with ProcessLifecycleOwner in a way so that Every Activity gets These callbacks. This is how it should works i guess if app is in background u will pop a notification of new message . Checkout ProcessLifecycleOwner.
I wrote some application that write to file the GPS location every 30 seconds.
When the application is not on focus i want to continue to write to the file.
How to do it ?
That's not possible due to the lifecycle of an Activity. As soon as you lose focus or hide it it will go into the corresponding state of onPause, onStop or even onDestroy if its not needed anymore.
What you have to do in order to fix it to start a Service. There are different kind of services and in your case an unbounded background service would do the job.
You possibly want to start it in the onStop() and stop it at onResume methods
See here for more android documentation on services
https://developer.android.com/guide/components/services.html
To get things to continu working after the app is send to the background, you would need to use a Service instead of a Activity.
Check out this link, the Android Developer website about services:
https://developer.android.com/guide/components/services.html
Also read this, about the fundamentals of Android apps:
https://developer.android.com/guide/components/fundamentals.html
Now I have a mp3 player that can play music, and I'm wondering should i use service instead of activity.
I have two ways to play music in background (continues to play when user press back button)
When user press back button, I override onBackPressed() and move the activity to background like the way user press home button, so when user launch my app again, it will be displayed as expected.
I read some article about overriding onBackPressed() and some people said that it would not a good way to do some works in background, the better way is using service. :). So my idea is when user press back button, I call finish() to destroy and release the activity, then I create a service that can play music from the point that activity is finished and a notification to help user can go back the main activity.
I think the first approach is very simple and easy to implement but not recommended. And the second one is better but more complex and I don't think this way optimizes memory rather then the first one.
I'm very wondering about which one I should use. Please tell me the better one among two ways above or if you have experience working on something similar lately, please tell your approach in this situation.
Thank you.
You should use a Service for that.
And ideally you should use a Ongoing Notification
This will prevent your service from being killed by the System.
Look at the mediaplayer guide
If I understood your question properly, you should use a service.
Please refer to the media player guide.
I am developing the game using andengine.
How i handle the game continue options after redirect to home screen(press home key ) but the timer in running in background.
How to handle the external events (Incoming calls, sms). In j2me i used hideNotify and show Notify . How i make in andengine
You must make all of actions that will save the state of your app in onPause() method of the Activity. This method calls when the new activity put in the head of activity stack.
I think you should read this article containing the fundamentals about the activity lifecycle which pretty much covers all you need to know about how to handle an application going into background, exiting, coming back to front: http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle
Is there a good way to check if an entire application (not an activity!) is closed by the user? I want to log the time a user spends using the application, so a simple activity onPause() ,onStop() or onDestroy() is not sufficient.
There are several ways an application can be closed, either the user pressing the home button, search button or simply leaving the application. Is there a unified (eg. simple) way to see if any of these things happened?
There are several ways an application can be closed, either the user pressing the home button, search button or simply leaving the application.
Neither of these actually closes the application. The activities will continue to run unless you explicitly call finish() or the system kills them when it runs out of resources.
Why is not sufficient to use onResume() and onPause()?
will overriding finalize() to the App class (that extends Application) help for this job ?
if not , maybe a reference inside this class to another class that has this method?