Mind,
I don’t know if this is your exact problem - but it may be something relevant to your issue.
Android application loses state after launching another intent - Stack Overflow
Issue 2373 - android - Activity stack behaves incorrectly during the first run of an app when started from Eclipse - Android Open Source Project - Issue Tracker - Google Project Hosting
Issue 5277 - android - Marketplace / browser app installer allows second instance off app - Android Open Source Project - Issue Tracker - Google Project Hosting
There are two ways suggested to avoid having multiple copies of an app run (as there was a bug with Google Play which if you run from the “Open” button there would launch a second version of the app, even if you already had the first version running - esp. if the first version was running another activity of that app at that time).
One way requires use of GET_TASKS etc.
The second way - which I had selected as perhaps the “better” version uses the launch intent - checks if it includes MAIN and LAUNCHER.
These two are taken from the above links - however the second one seemed better - and requires less permissions.
Basically you insert a first dummy Activity - that is mentioned in the AndroidManifest.xml as the first activity (MAIN and LAUNCHER etc. stuff).
This first dummy activity launches the real activity.
However in this first dummy activity you do the check - if the activity was NOT launched with MAIN and LAUNCHER intent, you do a finish().
This supposdly prevents (or quickly exits) a second run of the app - WELL BEFORE any of your other real and complex code within the “real app” actually gets to run.
The first activity mentioned in AndroidManifest.xml can be identified with this stuff:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This activity can be defined to have no history (since is a dummy first activity, would not want it to be visible in the activity history).
In AndroidManifest.xml define it to have:
android:noHistory="true"
For example:
<activity android:name=".FirstDummyActivity"
android:noHistory="true" ...>
From the onCreate() of your first dummy activity are going to check:
private static boolean isAMultipleLaunch(Activity activity) {
if (!activity.isTaskRoot()) {
// return the intent that stated this activity
// http://developer.android.com/intl/zh-tw/reference/android/app/Activity.html#getIntent%28%29
Intent intent = activity.getIntent();
// http://developer.android.com/intl/zh-tw/reference/android/content/Intent.html#getAction%28%29
// getAction()
// Retrieve the general action to be performed, such as ACTION_VIEW.
String action = intent.getAction();
// http://developer.android.com/intl/zh-tw/reference/android/content/Intent.html
// hasCategory()
// Check if a category exists in the intent.
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) &&
action != null &&
action.equals(Intent.ACTION_MAIN)) {
//----------
// is a multiple launch ..
return true;
//----------
}
}
//----------
// is not a multiple launch ..
return false;
//----------
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isAMultipleLaunch(this)) {
//----------
// exit
finish();
return;
//----------
}
// is NOT a multiple launch .. can move forward ..
// layout for this dummy activity etc.
setContentView(R.layout.forthisfirstactivity);
//----------
// start the real activity - let's say it's called "TheRealActivity.java"
{
Intent startIntent = new Intent(this, TheRealActivity.class);
startActivity(startIntent);
}
//----------
}