how to turn back to same activity instance - android

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.

Related

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

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.

What method is called when i change activities?

I have a MediaPlayer that keeps playing when I change activities and when I even click the physical Home button on my phone. How can I make it so that the MediaPlayer keeps playing when I change activities yet stops playing when I click the Home screen button?
You can use a service. that keeps running the background.
the service is similar to normal activities but they run in background and dont have a user interface

How to Resume Previous running application from An Application in Android?

Give me a little guidance:
as My Application is running with Broadcast Receiver and when ever my application in Background and any incoming event comes it triggers the my Application.
Now we want as We are listening or playing some video file or browsing on Android Phone
and with incoming event occur and my application launch(already running in Background),
If i will ignore the event , we want to go back previous running task (Like browsing, playing
video file application), How we can achieve?
Now I am using The store the running application package and Activity info before triggering the Event , and after finishing using this info we resuming the Activity and
It working fine but Only with Gallery Application Playing of Vedio file is not fine , It Crashes the Application and relaunch the Gallery App.
Please give me some Valuable points to make it up?
On calling finish, Previous application resume(Playing of Media file) but when next time
on getting broadcast intent and try to launch the Activity , The Activity is not launching
and application itself crashing.
After a long back I got solution of my problem and we need only use to this api
moveTaskToBack(true);
and automatically resumes the previous running task(like browser, media application or any thing)
Have you tried to start you activity by
startActivityForResult(Intent intent)
When you call finish() in the Activity you should return the the previous one, I am not sure if this will function over between Activity running in different applications. Give it a try :D

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

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