0

For example, if I launch my application and start activity Main and then start another instance of Main from a home screen widget, are these two instances of Main in the same task or not?

The reason I ask is because this intent does not clear the activity stack when launching activities from the widget

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
the_prole
  • 8,275
  • 16
  • 78
  • 163

1 Answers1

-1

I would suggest to use single instance mode using manifest.xml file.

<activity android:launchMode= "singleInstance" />

or try to launch your current activity:

 @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    public Intent launchIntent(Context ctx) {
        final ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        Intent intent = new Intent();
        boolean activated = false;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            List<ActivityManager.AppTask> tasks = am.getAppTasks();
            for (ActivityManager.AppTask task: tasks){
                intent = task.getTaskInfo().baseIntent;
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activated = true;
                break;
            }
        } else {
            final List<ActivityManager.RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0);
            String myPkgNm = ctx.getPackageName();
            if (!recentTaskInfos.isEmpty()) {
                ActivityManager.RecentTaskInfo recentTaskInfo;
                final int size = recentTaskInfos.size();
                for (int i = 0; i < size; i++) {
                    recentTaskInfo = recentTaskInfos.get(i);
                    if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) {
                        intent = recentTaskInfo.baseIntent;
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        activated = true;
                    }
                }
            }
        }
        if (!activated) {
            intent = new Intent(this, ABase.class);
        }
        return intent;
    }
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194