0

My MainActivity has a bottom navigation which defaults to the first item. Depending on data received via intent, I'd like for the selected item to change. I'm using the following code on the MainActivity's OnCreate method:

    if (getIntent().getExtras() != null) {
        String deepLink = getIntent().getExtras().getString("type");
        
        if (deepLink.equals("mydata")) {
            if (loggedIn) {
                bottomNav.setSelectedItemId(R.id.myitem);
            }
        }
    } else {
        setTitle("First");
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FirstFragment(), "First").commit();
    }

This works initially but after a second or two, the selected item goes back to being the first. What can I do so that the selected item remains active after loading?

cesarcarlos
  • 1,271
  • 1
  • 13
  • 33

1 Answers1

0

Try to put a delay before selecting the item

Here I put 100 milliseconds, you can increase that if it doesn't work

new Handler(getMainLooper()).postDelayed(new Runnable() {
    @Override
    public void run() {
        bottomNav.setSelectedItemId(R.id.myitem);
    }
}, 100); // 100 msec delay
Zain
  • 37,492
  • 7
  • 60
  • 84