NavUtils.shouldUpRecreateTask fails on JellyBean

后端 未结 3 1398
我在风中等你
我在风中等你 2021-02-13 10:22

I have an application that issues notifications that when selected start an activity. According to the Android docs I can use NavUtils.shouldUpRecreateTask to check whether the

3条回答
  •  我寻月下人不归
    2021-02-13 10:37

    I still don't know why shouldUpRecreateTask fails - looking at the source code for it doesn't help much. But the solution is quite simple - I just add an extra flag value to the Intent that is attached to the notification, and check this in onCreate(). If it is set, then the Activity has been invoked from the notification, so the back stack has to be recreated.

    The code looks like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle b = getIntent().getExtras();
        fromNotification = b.getInt("fromNotification") == 1;
        setContentView(R.layout.threadlist);
    }
    
    @Override
    public boolean onHomeButtonPressed() {
        if(fromNotification) {
            // This activity is not part of the application's task, so create a new task
            // with a synthesized back stack.
            TaskStackBuilder tsb = TaskStackBuilder.from(this)
                    .addNextIntent(new Intent(this, COPAme.class));
            tsb.startActivities();
        } 
            // Otherwise, This activity is part of the application's task, so simply
            // navigate up to the hierarchical parent activity.
        finish();
        return true;
    }
    

提交回复
热议问题