How to make FLAG_ACTIVITY_NEW_TASK ignore taskAffinity?

479 Views Asked by At

I have a problem with two of the buttons in a launcher app I'm developing. The first button should launch TotalCommander's built-in text editor to display a certain text file. The second button should launch TotalCommander. So I started with the code below...

ImageButton button1 = (ImageButton) findViewById(R.id.imageButton1);
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file:///storage/emulated/0/myNotes.txt"), "text/plain");
        startActivity(intent);
    }
});

ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent= getPackageManager().getLaunchIntentForPackage("com.ghisler.android.TotalCommander");
        startActivity(intent);
    }
});

...but it worked in an unexpected way:

  1. The first button opens the text-editor as expected.
  2. Then pressing the home button shows the launcher as expected.
  3. Then the second button opens TotalCommander as expected.
  4. Now pressing the back button shows the text-editor, instead of showing the launcher.

...so to be more specific: I want the buttons to use separate tasks, so that navigating back from either the text-editor or TotalCommander shows the launcher. (Except when the text-editor was opened from TotalCommander, in which case a new instance of the text-editor activity should be launched by TotalCommander in its own task.)

So I added intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) to both buttons...

ImageButton button1 = (ImageButton) findViewById(R.id.imageButton1);
button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file:///storage/emulated/0/myNotes.txt"), "text/plain");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
});

ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent= getPackageManager().getLaunchIntentForPackage("com.ghisler.android.TotalCommander");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
});

...but this results in the same behavior. I assume the FLAG_ACTIVITY_NEW_TASKs are ignored because of the task affinities of the TotalCommander activites. How could I make FLAG_ACTIVITY_NEW_TASK ignore taskAffinity, or modify the taskAffinity of the relevant TotalCommander activities?

0

There are 0 best solutions below