I am designing an app on the Android with a Navigation Drawer. Lets say I navigate through fragments and go from Fragment 1
to Fragment 2
. Everything works fine but when I am in Fragment 2
(which loads from the navigation drawer) and click the system back button althought I get the previous fragment (I use addToBackStack
) the navigation drawer doesn't get updated and the cell of Fragment 2
is highlighted. What should I do to fix this?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Found the solution:
Added a tag in every
addToBackStack
. So the code if I calladdToBackStack
it looks like this:whenever I put each fragment to the stack. Then I override the back button pressed:
The question is 2 years old and the answer of @alecnash is working. But in my opinion he is misappropriating the
onBackPressed()
method... and for later googler:Better use an
OnBackStackChangedListener
. In this approach you need not to override theonBackPressed()
which you probably need for somethingelse. Together with the code from @alecnash the listener looks like: