1

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?

Tamás Bolvári
  • 2,976
  • 6
  • 34
  • 57
  • 1
    Look into [`FLAG_ACTIVITY_NEW_DOCUMENT`](https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NEW_DOCUMENT) for the text file. Also `getLaunchIntentForPackage` already sets `FLAG_ACTIVITY_NEW_TASK`. – Eugen Pechanec Feb 02 '19 at 08:46
  • @EugenPechanec Thanks, but unfortunatelly it's the same with `FLAG_ACTIVITY_NEW_DOCUMENT`: the flags seem to be ignored, because of the `taskAffinity` attributes specified in the manifest of the TotalCommander app I guess. I'm not sure though. Do you think the intent flags can misbehave because of the task affinities of the two relevant TotalCommander activities (text editor and main activity)? – Tamás Bolvári Feb 02 '19 at 08:57
  • I've checked the manifest of [TotalCommander](https://play.google.com/store/apps/details?id=com.ghisler.android.TotalCommander) using [ManifestViewer](https://play.google.com/store/apps/details?id=jp.susatthi.ManifestViewer&hl=en_US). The text-editor activity doesn't even have `android:taskAffinity`, so my problem with the flags seems to be unrelated to task affinities... – Tamás Bolvári Feb 02 '19 at 09:12
  • 1
    So they all have the same default affinity. On the other hand, what you observe could be considered correct behavior. The launch intent typically (if the target activity has launch mode `standard`) puts you where you left off. And in this case you left off in the editor in TC... What can you see in the Recents screen when you launch TC and then the text with `NEW_DOCUMENT` flag? – Eugen Pechanec Feb 02 '19 at 09:12
  • @EugenPechanec If this is the correct behavior, how could I make it behave differently as if the text editor was a standalone app? So that the editor and the directory browser can have two separate tasks? – Tamás Bolvári Feb 02 '19 at 09:24
  • @EugenPechanec I see only one entry with a label "Total Comman..", displaying the contents of my text file (not the directory list). – Tamás Bolvári Feb 02 '19 at 09:26
  • Further examining the manifest, I've found a main activity without affinity and a directory browser activity with `android:taskAffinity=".DirBrowseActivity"`. I guess the latter is launched by my launcher's second button, but I'm not sure... – Tamás Bolvári Feb 02 '19 at 09:31
  • 2
    Don't make any assumptions. When you run `adb shell dumpsys activity` while in TC you'll see that the main activity is `com.ghisler.android.TotalCommander/.TotalCommander`. From my point of view, TC is written in such way that the editor opens in the same task as main app. This is not your responsibility. You're not expected to fix all the apps out there. Unless that's your added value. Avoid this problem if you can. If you're trying to show some sort of manual or read me, don't open an arbitrary *editor*. Open it in your own controlled activity. Otherwise the handling app is user's choice. – Eugen Pechanec Feb 02 '19 at 09:44
  • @EugenPechanec Indeed, my assumptions didn't help, but `adb` did, thanks to you. I develop this launcher for myself, not for the public. I try to keep it as small as possible (< 200 kB in its current beta state), so I'd be happy to reuse the editor of TC instead of making my own. If it's impossible the "fix" TC (making it use 2 separate tasks for the directory browser and the text editor), I'll create my own controlled activity as you advised. But is it impossible for sure? There must be a way to launch TC's editor in an exclusive task. – Tamás Bolvári Feb 02 '19 at 09:59
  • 1
    There's no way of overriding task affinity. Try this instead: Modify your TC launch intent and add `FLAG_ACTIVITY_SINGLE_TOP | FLAG_ACTIVITY_CLEAR_TOP`. Assuming everything in TC happens in the main activity it should do what you want. However, if you left the TC task with more activities on top, you'll lose them next time you start TC from your launcher. That's also why it's not broadly applicable - you want the launcher to bring you where you left off. – Eugen Pechanec Feb 02 '19 at 10:09
  • @EugenPechanec "_There's no way of overriding task affinity._" ⬅ This is sad, but a clear answer to my question, thank you, I'll accept it. I've already considered the clear top flag, but unfortunatelly not everything happens in the main activity in TC, so it would cause loss of data. – Tamás Bolvári Feb 02 '19 at 10:15

0 Answers0