开发者

Two instances of my android application are running....How to avoid this?

Here is my problem -

  1. I copied my .apk file onto phone memory card and launch my application clicking on it and it allows me to install my application.I install my application.Finally,I got system installation pop up containing two options "Open" and "Done".When i click "Open" my application got launched.Up to this point everything is working without any problem.

  2. Now in my application I click on a button and some download is taking place as a result(Showing progress dialog).Now I press a Home button,so my application goes to background.

  3. Now I again launch my application by going inside Menu and clicking on my application icon.

  4. Expected result - Still I Should see Progress Dialog for downloading. Actual resu开发者_运维问答lt - A new instance/session of my application is getting started.

So how to avoid this so that only one and one instance/session of my application should run.


@Palejandro, here you are. Put the code below into your main activity onCreate() method:

// 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 fine!


I believe you need to put

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

in the manifest file.


what do your OnPause, OnResume and OnCreate? I will bet money you are not saving anything OnPause, and starting a new instance all the time via OnCreate.

You should read the notes on Activity Lifecycles.


If you haven't got this sorted yet, I would say your app is actually being killed when home is pressed, or perhaps you have a bug that doesn't latch onto whatever object is keeping state.


// put below code in your launcher activity before call super and 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();
    }


I don't have a solution but the problem is that the intent used to start the app is different when you open it directly from install compared to opening it from your home screen. Since it will get started by two different intents it will open a new instance the second time round.

A quick work around is to avoid pressing "Open" when you have installed the application. Press "Done" and then find the application yourself.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜