我有一个带有DrawerLayout的活动(MainActivity
),使用NavigationDrawerFragment
作为导航菜单。在此NavigationDrawerFragment
中,点击导航项目时,系统会调用startActivity(intent to MainActivity with extra)
将onNewIntent
中收到的意图发送给MainActivity
;清单中android:launchMode="singleTop"
;显示相应的片段。在onCreate中,如果saveInstanceState不为null,则不会进行任何片段事务,因为当前片段将自动恢复。
问题是:对于一个片段(而不是其他片段):DashboardFragment
,默认情况下启动的片段,在屏幕方向上,片段将被恢复,但随后会重新创建。
我可以看到使用调试器:调用常规进程:重新创建活动,正确恢复片段,然后重新创建片段。
我也可以在FragmentManager日志中看到它(比较行为与另一个片段):片段被正确恢复但是,我可以看到提交用新的DashboardFragment
实例替换片段。在代码中,我无法看到此提交的来源(我在代码中的所有提交中都放置了断点,并且没有调用任何提交)。
你知道为什么会这样吗?
我可以根据要求提供代码,这是一个复杂的应用程序,不是开源的,我不确定将哪个部分放在这里。
简化主要活动:
public class MainActivity extends MeerkatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MeerkatApplication.get(this).getAppComponent().inject(this);
setContentView(R.layout.base_activity_layout_withdrawer);
setSupportActionBar(toolbar);
navigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigationMenu);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
toolbar,
R.string.open_drawer,
R.string.close_drawer);
drawerLayout.addDrawerListener(drawerToggle);
drawerLayout.setStatusBarBackground(R.color.primaryDarkColor);
drawerToggle.syncState();
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
}
//first calling before onNewIntent :
if (savedInstanceState == null) {
openFirstOpenableSection();
}
if (paneContainer != null) {
// Enable animation
LayoutTransition transition = new LayoutTransition();
transition.enableTransitionType(LayoutTransition.CHANGING);
paneContainer.setLayoutTransition(transition);
}
}
private void openFirstOpenableSection() {
int dc_id = -1;
if(getIntent() != null){
dc_id = getIntent().getIntExtra(EXTRA_DATACENTER_ID,-1);
}
int sectionToOpen = findFirstOpenableSection(sessionInformations
.getOrganizationInformations());
eventBus.post(new NavigationChangedEvent(sectionToOpen));
switch (sectionToOpen) {
case R.id.dashboard:
if(dc_id != -1){
IntentStarter.showDashAfterRequest(this,dc_id);
}else{
IntentStarter.showDashboard(this);
}
break;
...
default:
IntentStarter.showNoSectionAvailable(this);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
Bundle extras = intent.getExtras();
if (extras != null) {
String showAction = intent.getStringExtra(EXTRA_SHOW_ACTION);
if (rightPane != null) {
rightPane.setVisibility(View.GONE);
}
switch (extras.getInt(EXTRA_MENU_ITEM, -1)) {
case EXTRA_MENU_ITEM_DASHBOARD:
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, DashboardFragmentBuilder
.newDashboardFragment(sessionInformations.getUser().getId(),
getResources().getString(R.string.dashboard_personalDashboard),
"pda"), "DASHBOARD")
.commit();
}
drawerLayout.closeDrawer(GravityCompat.START);
break;
...
case EXTRA_MENU_ITEM_LOGOUT:
logout();
break;
default:
break;
}
}
}
IntentStarter:
public final class IntentStarter {
public static void showDashboard(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_MENU_ITEM, MainActivity.EXTRA_MENU_ITEM_DASHBOARD);
context.startActivity(intent);
}
}
NavigationDrawerFragment:
public class NavigationDrawerFragment extends MeerkatFragment {
@OnClick({R.id.dashboard, ... R.id.logout})
public void onMenuItemSelected(View view) {
switch (view.getId()) {
case R.id.dashboard:
desactivateAllViews();
IntentStarter.showDashboard(getActivity());
view.setActivated(true);
break;
case R.id.logout:
IntentStarter.logout(getActivity());
view.setActivated(true);
break;
...
}
}
}