Search code examples
androidandroid-bottom-nav-view

Can't set active item of BottomNavigationView while recreating app


Can't set active item of BottomNavigationView while recreating app: the last selected item is active after recreating app, so BottomNavigationView.setSelectedItemId(R.id.item0) takes no visual effect, but onNavigationItemSelected was called succeessfully.

So how can I prevent restoring previous active item of BNV? Thanks.


Solution

  • You need to do that inside onResume() instead because while your app in the recent apps then the BottomNavigationView will always catch the position.

    Why set position not work?

    Because you set the position inside onCreate() which not invoked when app start from recent apps.

    How to set the position then?

    Inside in onStart() or onResume() and that will work.

    How to check if app opened from recent-apps?

    We can play with boolean e.g:

    boolean isFromRecents = false;
    
    onCreate(){
    isFromRecents = true;
    }
    
    onResume(){
    if(isFromRecents)
       //do smth
    }