NavUtils.navigateUpTo() does not start any Activity

前端 未结 9 2145
陌清茗
陌清茗 2021-01-30 06:48

I have two activities

  • MainActivity
  • DeepLinkActivity

I set up everything to use the NavUtils for navi

9条回答
  •  佛祖请我去吃肉
    2021-01-30 07:16

    It looks like NavUtils.shouldUpRecreateTask(this, upIntent) is not prepared for this special case.

    My current workaround is checking, if the Activity was deep linked and force a new task stack for cases like this.

    In my case, I pass around objects in EXTRAs internally. But an ACTION and Uri is set if the Activity is started from outside by following a link to a specific URL or by touching NFC aware devices.

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent upIntent = NavUtils.getParentActivityIntent(this);
                if (NavUtils.shouldUpRecreateTask(this, upIntent)
                        || getIntent().getAction() != null) { // deep linked: force new stack
                    // create new task
                    TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent)
                            .startActivities();
                } else {
                    // Stay in same task
                    NavUtils.navigateUpTo(this, upIntent);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    

提交回复
热议问题