我有一个带有多个片段的活动,一个片段用于不同的动作或数据表示,包括一个带有MapFragment
的片段,用于跟踪用户的位置。导航通过BottomNavigationView
。
我发现无论何时重新调整屏幕方向,“活动”都会重新启动默认的“片段”,无论我是否设置
setRetainInstance(true);
有趣的是,该实现在我使用BottomAppBar
的另一个应用程序中有效。我猜想BottomNavigationView
会改变一切,但我无法完全理解。
活动中的片段加载代码如下:
OnCreate
FragmentManager manager = getSupportFragmentManager();
if (manager.findFragmentById(R.id.frameLayout) == null) {
Log.d(TAG, "onCreate: transaction manager state is Empty: "+ manager.beginTransaction().isEmpty());
// instantiate fragment and add to view
Fragment mFragment = new ReviveFragmentIncidentDashboard();
manager.beginTransaction().replace(R.id.frameLayout, mFragment ).commit();
} else {
// Do nothing
}
OnNavigationItemSelectedListener
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener () {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId ()) {
case R.id.navigation_dashboard:
//mTextMessage.setText (R.string.title_home);
switchToFragment1();
return true;
case R.id.navigation_home:
//mTextMessage.setText (R.string.title_visits);
switchToFragment2();
return true;
case R.id.navigation_settings:
switchToFragment3();
return true;
// default: // not sure if this needs to be there
//switchToFragment1();
}
return false;
}
};
编辑/更新:
我还尝试过通过使用片段的onSaveInstanceState来手动检查正在被破坏的片段,但是由于某些奇怪的原因,onSaveInstanceState没有被调用/ outBundle没有使用片段的TAG更新。
片段代码
private static String TAG = "FragmentHousecallDashboard";
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("fragtag", TAG);
}
活动代码: 在OnCreate中
FragmentManager manager = getSupportFragmentManager();
Fragment mFragment;
if(savedInstanceState == null) {
mFragment = new FragmentIncidentDashboard();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
} else {
Log.d(TAG, "onCreate: savedInstanceState : " + savedInstanceState.toString());
String fragtag = savedInstanceState.getString("fragtag");
switch(fragtag){
case "FragmentHousecallDashboard":
mFragment = new FragmentHousecallDashboard();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
case "FragmentHousecallRequestList":
mFragment = new FragmentHousecallRequestList();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
case "FragmentIncidentDashboard":
mFragment = new FragmentIncidentDashboard();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
case "FragmentIncidentList":
mFragment = new FragmentIncidentList();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
case "FragmentSettings":
mFragment = new FragmentSettings();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
case "FragmentSettingsAbout":
mFragment = new FragmentSettingsAbout();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
case "FragmentSettingsAuthentication":
mFragment = new FragmentSettingsAuthentication();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
case "FragmentSettingsBehavior":
mFragment = new FragmentSettingsBehavior();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
case "FragmentSettingsPayment":
mFragment = new FragmentSettingsPayment();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
case "FragmentSettingsPersonal":
mFragment = new FragmentSettingsPersonal();
manager.beginTransaction().replace(R.id.frameLayout, mFragment).commit();
break;
}
}