简体   繁体   中英

Weird behaviour : Activity not getting started again when first started with FLAG_ACTIVITY_NEW_TASK

My scenario is,

When application starts then after user logs in I am starting an activity in the following manner,

Intent rangeFinderScreen = new Intent(YourLocationScreen.this, RangeFinderScreen.class);
rangeFinderScreen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(rangeFinderScreen);

I am using Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK so that all previous activity stack gets cleared and it starts as a new task.

Then I have bottom bar to navigate between activities, so I have made a common listener (Activity) and implemented that in all the activities. It looks like the following,

public class CommonListenerForBottomButtons extends Activity implements View.OnClickListener{

    Context context;
    String buttonName;

    public CommonListenerForBottomButtons(Context context, String buttonName){
        this.context = context;
        this.buttonName = buttonName;
    }

    @Override
    public void onClick(View v) {
        switch(buttonName){
            case "play":
                Intent rangefinder = new Intent(context, RangeFinderScreen.class);
                rangefinder.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(rangefinder);
                break;

                // and so on more cases

        }
    }
}

Concern

If while starting the activity for first time I do the following,

Intent rangeFinderScreen = new Intent(YourLocationScreen.this, RangeFinderScreen.class);
startActivity(rangeFinderScreen);

Then all works .

But if I do it the following way (as I am doing now), ie by using rangeFinderScreen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Then activity doesn't get started again.

However if I start activity from any other activity rather than the Common Listener Activity like,

Intent rangefinder = new Intent(MyGolflerScreen.this, RangeFinderScreen.class);
startActivity(rangefinder);

Then it starts .

EDIT

Below is how I am initializing Common Listener in all activities:

homeLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "home"));
playLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "play"));
weatherLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "weather"));
messageLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "message"));
myGolflerLinearLayout.setOnClickListener(new CommonListenerForBottomButtons(getApplicationContext(), "mygolfer"));

All other activities start except for the one which is initially started with the flag Intent.FLAG_ACTIVITY_NEW_TASK .

Manifest file entry

<activity
    android:name=".RangeFinderScreen"
    android:label="@string/title_activity_range_finder_screen"
    android:screenOrientation="portrait" >
</activity>

Do not set the flag Intent.FLAG_ACTIVITY_NEW_TASK when you launch an Activity from another Activity within your application. This isn't necessary and is probably causing your problem.

Also, if you are starting a new Activity every time the user clicks on a button you will find that you are creating new instances of these activities every time, and will end up with a big stack of them after awhile. This probably isn't the desired behaviour either. To switch between activities that may already be in the task stack you can set the flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT .

If your concern is to manage all stacks i prefer to use all "singleTop" Activities and starting them with flag Intent.FLAG_ACTIVITY_CLEAR_TOP It will manage all you want to do.

you can add launchMode in Manifest inside activity tag that is equal to "singleTop" .

Hope it works ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM