NavUtils.navigateUpTo() does not start any Activity

前端 未结 9 2124
陌清茗
陌清茗 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:25

    It doesn't work for me too. So I handle up this way:

       @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item.getItemId() == android.R.id.home) {
    
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                    finish();
                    return true;
    
            }
            return super.onOptionsItemSelected(item);
        }
    

    My MainActivity is marked as singleTask in android manifest

    android:launchMode="singleTask"
    
    0 讨论(0)
  • 2021-01-30 07:32

    Try this :

    @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)) {
                    // create new task
                    TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent)
                            .startActivities();
                } else {
                    // Stay in same task
                    NavUtils.navigateUpTo(this, upIntent);
                }
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }
    
    0 讨论(0)
  • 2021-01-30 07:33

    I think that method is bugged. I've read support library source code, and that method check for intent's action. It only works when your App was previously created..as you've described, if you kill it from Apps preview, shouldUp method stops working.

    I've fixed this using my own "shouldUpRecreateTask". When I receive a Notification that creates directly an Activity (Like your behaviour), I send from my BroadCastReceiver a custom Action inside the intent. Then, in my Method I do the next thing:

    private final boolean shouldUpRecreateTask(Activity from){
        String action = from.getIntent().getAction();
        return action != null && action.equals(com.xxxxxx.activities.Intent.FROM_NOTIFICATION);
    }
    

    ..........................

     if (shouldUpRecreateTask(this)) {
          TaskStackBuilder.create(this)
           .addNextIntentWithParentStack(upIntent)
           .startActivities();
     } else {
           fillUpIntentWithExtras(upIntent);
           NavUtils.navigateUpTo(this, upIntent);
     }
    
    0 讨论(0)
提交回复
热议问题