Two instances of my Android application work ... How can I avoid this?

Here is my problem -

  • I copied my .apk file to the phone’s memory card and launched my application by clicking on it, and this allows me to install my application. I am installing my application. Finally, I got a system installation popup containing two options: Open and Finish. When I click "Open", my application starts. Up to this point, everything works without problems.

  • Now in my application, I click on the button, and as a result, the download occurs. (Shows the execution dialog). Now I press the "Home" button, so my application goes into the background.

  • Now I launch my application again by going to the Menu and clicking on the icon of my application.

  • Expected Result. However, I should see the Progress dialog box to download. Actual result - a new instance / session of my application begins.

So how to avoid this in order to start only one and one instance / session of my application.

+5
source share
6 answers

I believe that you need to put

<activity
    android:launchMode="singleInstance"
/activity>

in the manifest file.

+8
source

@ Paleandro, there you are. Put the code below in your main activity onCreate() :

// Possible work around for market launches. See
// http://code.google.com/p/android/issues/detail?id=2373
// for more details. Essentially, the market launches the main activity
// on top of other activities.
// We never want this to happen. Instead, we check if we are the root
// and if not, we finish.
if (!isTaskRoot()) {
    final Intent intent = getIntent();
    final String intentAction = intent.getAction();
    if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
        Log.w(TAG, "Main Activity is not the root. Finishing Main Activity instead of launching.");
        finish();
        return;
    }
}

I used this piece of code in my projects and it works great!

+8
source

OnPause, OnResume OnCreate? , OnPause OnCreate.

Activity Lifecycles.

+1

, , , , , , , - , .

0

// super setcontentview()

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);

    // get the info from the currently running task
    List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(10);
    boolean alreadyTask=false;
    for(ActivityManager.RunningTaskInfo info : taskInfo){
        ComponentName componentInfo = info.topActivity;
        String value= componentInfo.getPackageName();
        if(value.contains(getPackageName()) && !info.topActivity.getClassName().contains(getPackageName()+".LauncherActivity")){
            alreadyTask=true;
            Log.i(TAG, "second instance found!!!");
            break;
        }
    }

    if(alreadyTask){
        finish();
    }
0

I have no solution, but the problem is that the intention used to launch the application is different when you open it directly from the installation compared to opening it from your home screen. Since it will start with two different intentions, it will open a new instance a second time.

Quick work - avoid clicking "Open" when you installed the application. Click Finish, and then find the application yourself.

See: http://code.google.com/p/android/issues/detail?id=2373

0
source

All Articles