Alternative to Intent.FLAG_ACTIVITY_CLEAR_TASK

后端 未结 6 1080
傲寒
傲寒 2021-02-06 22:59

I have two apps App-B launches App-A. If the user starts App B from inside App A I call finish on App-A so I have no problem.

If the user goes straight to App B from th

相关标签:
6条回答
  • 2021-02-06 23:04

    The new IntentCompat should've helped on that, but apparently the flag is ignored for API lower than 11.

    To use IntentCompat do following:

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
    IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
    
    0 讨论(0)
  • 2021-02-06 23:05

    I may be wrong in understanding what you are asking, but is it that when you launch B, you want A to be killed?

    In A, add this to the activity tag in the manifest:

    android:noHistory=true
    

    This will cause the activity to be removed from memory as soon as it loses focus.

    0 讨论(0)
  • 2021-02-06 23:10

    The best documentation I've found for these Intent flags is here: http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/

    I don't understand what you are trying to do, but have you tried FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET?

    0 讨论(0)
  • 2021-02-06 23:16

    I'm still having a lot of trouble understanding the problem but would like to help you fix it. Since comments only allow 600 characters and don't format well, I'm going to create an answer instead, because I'm sure that together we can get this resolved.

    I would like to be able to reproduce your problem. To do that I've created 2 applications: AppA and AppB. AppA has a single activity called ActivityA and AppB has a single activity called ActivityB. Both ActivityA and ActivityB use android:launchMode="singleTask".

    ActivityA has a button on it that launches AppB and finishes, like this:

        Intent intent = new Intent("de.sharpmind.example.AppB");
        intent.putExtra("extra", "launched from AppA");
        startActivity(intent);
        finish();
    

    ActivityB has a button on it that launches AppA like this:

        Intent intent = new Intent("de.sharpmind.example.AppA");
        intent.putExtra("extra", "launched from AppB");
        startActivity(intent);
    

    This all works as I expect it to. AppA and AppB run in different tasks. The "extra" is properly seen in the onCreate() methods of each application.

    So, can you please tell me more about your problem. How can I reproduce exactly your problem? You wrote:

    On lower APIs the new task in APP-A will not change and extras putExtra will have no effect.

    What do you mean by that? Where are you putting extras and where are you getting them and what do you expect to happen?

    Also, what is the launchMode of your AppB?

    Also, when you have this problem, are there other activities in the task stack for AppA?

    Please provide more info, either in your original question or here as comments.

    0 讨论(0)
  • 2021-02-06 23:24

    this will work correctly

    i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY | 
                   Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    
    0 讨论(0)
  • 2021-02-06 23:30

    Using FLAG_ACTIVITY_CLEAR_TASK clears the back stack. If I'm understanding correctly, this is the behavior you want.

    Using singleInstance instead of singleTask in your manifest will do this.

    In the comments you said that it must be singleTask. I'm assuming this is because you need the back stack in certain circumstances.

    Since launchMode can't be changed programaticaly and FLAG_ACTIVITY_CLEAR_TASK is not availble for API <=10, you may have to create two identical activities.

    One with launchMode=singleTask and one with launchMode=singleInstance.

    Add this to the one using singleInstance to get a clear stack when launched from the app drawer:

     <category android:name="android.intent.category.LAUNCHER" />
    
    0 讨论(0)
提交回复
热议问题