Android Bottom Navigation - Maintaining Activity State

无人久伴 提交于 2020-01-26 03:14:28

问题


I know it's recommended to use Fragments instead of Activities when working with Bottom Navigation. Due to the current design, I want to avoid having to convert all the Activities

The Design: I am using a Bottom Navigation bar like shown

Each tab is an Activity. When you tap on any of the tabs, it launches the activity, startActivity(new Intent(getApplication(), TabActivityName.class));. The problem is that when you switch between these tabs, the state of the Activity is lost.

For example:

  1. Click on the Music tab (Shows a list of artists)
  2. Click on an artist in the Music tab to show the Discography fragment.
  3. Switch to the Video tab
  4. Switch back to the Music tab. (I want the Artist's discographyFragment to still be showing, but it is back at the main List of Artists fragment)

Things I've tried:

  1. Changing each of the tab Activities to android:launchMode="singleTask" and android:alwaysRetainTaskState="true" This only preserves data if you are switching to tabs that were started before the current activity.
  2. Changing each of the tab Activities to android:launchMode="singleInstance" and android:alwaysRetainTaskState="true" But this creates an undesired effect of creating multiple application tabs.

Any other ideas on how to maintain the state of the Activity and which Fragment is loaded when switching tabs?


回答1:


You could use FragmentStatePagerAdapter in the Activities. However, you should update to fragments, they are designed to handle your situation. The migration is not that bad, most of the logic can simply be copied over.



来源:https://stackoverflow.com/questions/44011805/android-bottom-navigation-maintaining-activity-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!