I am trying to limit a family of applications, using the same activity (in a Library Project shared by all), so that only one such application can run at a time.
In my first round of trying to learn how to go about that, I learned that:
- android:launchMode="singleTask" won't do the trick.
setVisible(true/false)
in onResume()/onPause() respectively is the best way to tell whether my application is running. But what do I do with it once the 2nd instance knows that the 1st instance is running???
So, I am thinking of tricking the system by always returning the same instance by overriding Activity.getApplication() and returning a shared static:
public class MyApplication extends Application
{
private static MyApplication s_instance;
public MyApplication()
{
s_instance = this;
}
public static MyApplication getApplication()
{
return s_instance;
}
}
(credit for the idea goes to @inazaruk)
But, like the aforementioned visibility solution, what do I do with this now?
Does the system itself use getApplication() to determine whether to launch a new instance or just bring it to the front of the stack? Is this safe?
If the answer is "no", can I just call Activity.finish() to not let the 2nd activity not even start? If so, where do I do this?
- Right in Activity.onCreate()?
- Activity.onResume()?
- MyApplication()? (constructor)
- A better place?