Open the same app instance - android

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

Related

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.

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.

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

No Background Application in Android

I am making an application.
Now what i want is that whenever user Press HOME key on android, it does not go in Background Mode.
Can any body give any suggestions how to implement all that?
Please Reply.
Thanks a bunch in advance
You cannot (easily) force your app's process to exit, nor should you. You can force an activity to disappear from the stack when the user navigates away from it by adding android:noHistory="true" to the manifest for that activity. But that will apply even to another activity in your app. You can also add android:finishOnTaskLaunch="true" to force an activity to be closed down if the user launches it again from the home screen. However, the activity will run until then (or until the system shuts down the process for other reasons).
Android naturally keeps your application alive, but calls the onPause because it's not an active activity, then calls onResume when the user goes back to your application.
If you need to run code without a UI or you want it to be checking something then opening your app at some point You can use a service.
API Demos have some great examples of how to use a service

How to close an application when the Home button is pressed

Hi I have application with more than 20 activities.I want to close my app when Home button is pressed.
There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.
You could use the launchMode and clearTaskOnLaunch flags on your root activity from your AndroidManifest.xml:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
When you again start your app, all activities will be killed.
You don't want to do System.exit() -- that's not how the Android Activity Lifecycle normally works (read this also).
What you should do is move the App to the background with moveTaskToBack(). Android will then keep your app running in the background, and kill it if it's unused and something needs its resources later.
If you want it to close all of the open Activities when your App is no longer visible, you can set noHist = "True" for all of the child activities (leave the main activity with noHist = "False", though). This will make it where instead of reopening your application on the last Activity they were on, it will open it on the "main" activity (i.e. it will be as if they just restarted the app).
Anyhow, read through the following answers for more information: Close application and launch home screen on Android
I have the same problem. Im writing a banking app and am required, by contract, to log off the user (or exit) when the app is put into background. There are obvious security concerns there.
There are a couple of ways Im looking to do this:
1. Intercept home button (and back button for the root activity) key press events to call logoff and/or finish()
2. In the onStop() method, for every activity, detect whether the activity is being stopped due to a new activity being show - if not, assume app is being put to background so logoff and/or finish()
The first may not work if a notification is brought to the front then the user clicks home (I havent investigated yet). Or maybe there are other ways for an app to be put into the background without pressing these buttons
The second way sounds messy & difficult to maintain
Id welcome any other ideas
Drapes
I know android has been designed this way, but it seems naive to think that apps wouldnt want an applicationOnStop event
Hi guys what I understood is that u need to know when app goes in background and how to detect it and if I am wrong plz correct me----
The user can go in background if ur app does not provide any way by pressing Back key or Home Key.
You need to use methods "dispatchKeyEvent(KeyEvent event)" to get home key event or back key event and after getting the event you can execute your task.
you can even restrict user from pressing any key but u can not control the home key.

Categories

Resources