开发者

How to avoid multiple instance of activity starting from service

I am working in a service, on device boot my service starts along with application. Now if service finds some restricted apps launched, it backgrounds my application, shows home, kill app, and start again activity to foreground. But it开发者_StackOverflow is creating multiple instances when starting activity on boot.

I want to start previously launched activity every time if it exists, I am using the following code:

    //activity to background    
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);

    //killing restricted app    
    Appmgr.killBackgroundProcesses(RunningP.processName);

    //again start activity      
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ComponentName cn = new ComponentName(this, TaxiPlexer.class);
    intent.setComponent(cn);
    startActivity(intent);

I have used FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY and others but they are giving me force close error. Above code only creates multiple instances ON BOOT


add this to manifest:

<activity android:name="yourActivity" launchMode = "singleTask" ></activity>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜