Do not allow multiple instances of an activity of an Android app - android

I'm trying to create a really simple application for Android that will play a music file (I'm really only just starting to get into Android). I have only one Activity that starts when the application starts, and it starts playing the music file. What I need is that the activity always runs (plays the music), whether you press Back or Home buttons, unless you specifically tell it to shut down from Settings menu, and if you try to run it again, it should just restore that activity to the front (basically, how every other player out there works). What happens for me, though, is that when I press back to return to the menu screen for instance, and click on the app again, it runs another instance of the activity (which I can tell, because the music doubles). What can I do to prevent this? Many thanks.

For playing music in the background I would recommend you using a service.

Specify android:launchMode= "singleInstance" in your manifest file. This means that your activity is your entire application.
Don't forget to save the state of your time of the music. Use SharedPreferences for saving an integer with the second when the sound ended playing and just restore the state in onResume() method.
Unfortunately, you cannot play music after you press back button as the activity is destroyed. You must start a service if you wish to do that as the other answer suggests. The reason is that you need a Context object to play music and it will no longer be available after onDestroy() method is called.
http://developer.android.com/guide/topics/media/mediaplayer.html
Here you can find examples of playing media files in a service.

Related

Background Music in App across Activities

I saw lots of similar questions, but I couldn't figure out the best way in my case.
I want to play some background music in my app (looped). It should start (if a sharedPreference is set) at the start of the app. If the user disable a checkbox, it should stop (if he enabled it, it should start again, preferences is set here...).
Nearly everything worked fine by using an MediaPlayer. But I don't know, how to stop the music if the user tabs the home button. Because of playing music across Activities, I think it doesn't seem wise to stop in onPause() or onDestroy()?!
I read a lot about AsyncTask and Services, because I know it's not good to play the music in the MainThread instead of an WorkerThread. But I couldn't figure out the best solution and how to handle it.
Is there a way to play music until I say explicitly "stop" (in any Activity) or the user tabs the home screen button without editing each Activity?

how to turn back to same activity instance

I have made a Mediaplayer (just play songs from SD card) and it is not a service but a single activity. by pressing the home button the player continues playing in the back ground.
now my problem is how to get back to the same instance of the activity by launching the application. mine starts a new activity while the previous instance of mediaplayer is still playing the song.
I have seen many similar questions but mostly was about the services specially in mediaplayer cases.
is it possible to set some flags onDestroy and check them again onCreate or somthing similar ?
thanks
mine starts a new activity while the previous instance of mediaplayer is still playing the song
Yes, this is a behavior, on start - new Activity has been created.
I have seen many similar questions but mostly was about the services
There is no way, run Player in Service in order to bind new created Activity with the Player
Try using the launch mode - single instance, for your activity.
android:launchMode=[ "singleInstance"]
in your Media Player activity in Android Manifest.xml file. It might work.
If you press HOME to put the application in the background, and then launching the application creates a new Activity, you are probably seeing this long-standing nasty Android bug: https://stackoverflow.com/a/16447508/769265
To check if you are seeing this bug, just force close your application, then launch from the HOME screen, start playing sound, press HOME button, then launch application again from HOME screen. It shouldn't create a new instance of your activity.
Pleae don't set launchMode="singleInstance". This is not necessary and will create more problems than it will solve.

Open the same app instance

I have a simple, one Activity application.
The problem is that, if i press the BACK button, the application is minimized, but if i try to launch it again, another instance is started.
I know this because my app has plays a sound stream even if is minimized.
How can i maximize the already running instance when trying to launch it ?
I've tried with the code below, but is not working.
android:launchMode="singleInstance"
Android handles this for you. What is probably happening is you're replicating your objects in onCreate thus the "appearance" of 2 activities via a 2nd sound stream.
I don't know what is the C button ? If it is the "back key" so your application is unload from the system, when you start it again, it opens a new instance.
If you press the 'middle key', normally there is only 3 keys on a Android phone, your application is just paused and put in the background, and when you launch it again, the sytem just put your running application from the background to the foreground. No new instance is opened. There are the methods onPause() and onResume() which are triggered in this case.
Hope it can answer to your question

Android: Confused about application states

I have seen this post here (http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state). I am having a similar problem I believe.
I have an app which listens to an audio stream (using the mediaPlayer object). If I press the Home button it will continue streaming and hide my app. Then, at a later point I can go back to my app and press stop when I'm done. This is what I want. If however I press the Back button, when I later open my app again the app has been redrawn from fresh. Text boxes, buttons, everything has reset like I've just opened the app for the first time so I can't stop my audio stream. Clicking stop does nothing because the app has 'forgotten' it is streaming (the stream runs under a separate handler from the main UI thread, so I'm guessing since its been 'reset' it has lost track of its handlers?).
Why does this happen with the Back button, and how can I stop it?
Move the streaming functionality into an Android Service. Use an Activity to bind to the Service and to interact with it.

I lose control Android's Webview when resuming the application

basically, what am doing is that am running a flash file (playing music) in the background of the app using webview, and when I want to stop the music , I just load "about:blank"
I want to keep the flash file running (music) when my app gets paused (which works fine so far) the problem is when the app resumes, pressing on pause button launches a new webview instead of changing the link in the previous one and the sound doesn't stop
the only solution I found was to kill the process and restart the app, but that's not practical, any idea of how to still be able to use the same webview when resuming the app ?
Update: guess I wasn't clear enough, I have a webview widget stated as "gone" , I only need the audio form the flash file I'm running in the webview so the "play" button loads a URL that contains the flash file (so the user only hears music, and doesn't notice that I'm using a webview), when I press the back/home button the music doesn't stop (I like it that way) but when I go back and press on play again it loads another url in a new page, you get double music
There are a few possible causes for this. One is simply that multiple instances of your activity are being created. Assuming you don't want this, add android:launchMode="singleTask" or android:launchMode="singleInstance" to your main <activity> in AndroidManifest.xml. More info here:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
If that doesn't help, another possibility is that your application is creating a new instance of the WebView -- either because the relevant code is in the wrong place (e.g. onStart()), or because the app has actually been destroyed and re-created by Android rather than paused and resumed -- and the old WebView thread is still hanging around for some reason. More information (such as code samples) would be helpful in identifying the problem, if that's the case.
Not sure about this solution but at least you should try this once
protected void onResume() {
super.onResume();
activityTermsAndConditionBinding.webView.releasePointerCapture();
activityTermsAndConditionBinding.webView.invalidate();
}

Categories

Resources