How to prevent launching of second copy of application? - android

I need to guarantee that my Android application won't be launched twice. How to do it? I mean in ideal I would need in case of second launch just switch to primary copy.

Consider using android:launchMode More on that here

According to the Android activity lifecycle, it shouldn't be possible to start a second copy on an activity. If it isn't terminated, it will resume the running instance.

That is the normal behavior of app launching. You don't need to do anything special. (And definitely do not at all use the android:launchMode="singleInstance" option. That is a very special behavior that is probably not what you want.)

Related

How to limit android app to a single instance?

I have an app that could be launched by a user or by the system (based on its main activity intent). So, in some cases there might be two instances of the app running at the same time.
Is there a way to limit the app to a single instance?
Thanks.
Add android:launchMode= "singleInstance" to your activity.
Hope it helps.

android and multiple running of the same application

I write application on android which will be runnig all the time on background. There will be only one starting view on first run. I want to user run instance of my app only once, and cant run any other instance at the time. When he try to run this app when one instance of this app is running already he sould see some warning notification. My problem is I dont know how to prevent user from multiple start of my app. Is this possible? If it is possible, how can I do it? Thanks for any help.
For background processing I would recommend to consider Services. Services are created to deal with longterm background tasks. I think foreground service(like Skype) may be interesting for you.
As Phil suggested you can you launch mode to control your activity behaviour. Consider using launchMode = "singleTop"
You need to pick a launchmode that will best fit your needs (probably singleInstance or singleTask). As for popping a notification, you can handle that in onCreate or onResume, however it doesn't have anything to do directly with how many instances are running.

ANDROID activity force stop on restart

I've looked through a lot of other threads about this but haven't had any luck yet with their solutions.
When I launch my app for the first time, I noticed that onResume is being called after onCreate. (not sure why)
My problem is when I try to run the application for the second time. Neither onCreate nor onResume get called, it just sits there.
ALso, I have these setting set in my manifest:
android:clearTaskOnLaunch="true"
android:screenOrientation="landscape"
android:noHistory="true"
If I ever want to run my app more than once, I have to go into settings->applications->app-name->force-close
(I don't have access to look for any loops or bad bugs that might be causing the sticking because I'm using a library with it's own threads.)
Is there a way I can fix this?
Call Process.sendSignal(Process.myPid(), Process.SIGNAL_KILL); in your onDestroy method. This is not good but the best solution for this problem.
This is the way android manages the activities. If you want your activity go through whole life cycle, call explicitly its finish() method.

How to launch first acitivity in the android application on some condition?

While application will be started, I want to launch one activity if some condition is satisfied else want to launch another activity. How to do this?
Start the one, and in onCreate() check the condition. If it is not satisfied start your another activity.
you must do it handling all conditions in an initial activity, which will check your rules and start the other activitys.
I don't know much about your particular use case, but if your application has to launch different activities depending on the task it is supposed to do, you should consider having appropriate intent-filters for each, and sending an Intent that's in accordance to the intended effect. That will make the correct activity be launched depending on the Intent, which is one of the great features of Android.
But of course, if you can only decide which activity is best once you launch your app, this approach doesn't work, and you have to launch an initial activity to decide which one to call next.
If it's highly likely that one activity will be launched, you might consider launching it as the first activity and then testing whether you need to launch another instead. That's probably cleaner than having a separate activity just to make the decision about which other activity to launch.
Bruno Oliveira, Developer Programs Engineer, Google

Destroy Android application

I have developed an application which is working fine.
In that i have used some static variables and also set Application level variables.
My problem is that even after setting finish() on each activity, application is showing in Running mode.
After closing the application, when i start the application after sometime, it will set the last changes.
How can i destroy my application?
I think the correct answer is "you shouldn't".
It doesn't matter if your app is still running after the user stops using it. Android will kill it when/if it needs to.
Your app most likely shouldn't even have an exit button.
I have checked that application does not release the resources.
So, what i have done is :
Process.killProcess(Process.myPid());
It will kill the application.
It helps me a lot.
call
System.exit(0);
from within
OnDestroy()
And yes, you shouldn't.
I hope you have called onDestroy for your activity.
Secondly, are you saving your preference some where else??
Please go through these methods used for the destroying the activities
setResult() finish() and onActivityresult()
After calling setResult() call finish() for the activity that you want to finish & then in it's parent activity override onActivityResult() in this you can finished your application.
The solution given by oli is right android will destroy your application running. But I want add, only if it is lacking in system resources.
For more help please go through following link.
http://developer.android.com/reference/android/app/Activity.html

Categories

Resources