I'm getting the above error when calling finish() and then re-opening the app. Here's the code that manages navigation onBackPressed().
android.app.FragmentManager settingsFragmentManager = getFragmentManager();
FragmentManager fragmentManager = getSupportFragmentManager();
if (settingsFragmentManager.getBackStackEntryCount() > 0)
settingsFragmentManager.popBackStackImmediate();
else if (fragmentManager.getBackStackEntryCount() > 0)
fragmentManager.popBackStackImmediate();
if (fragmentManager.getBackStackEntryCount() == 0 && endIfEmpty) finish();
if (fragmentManager.getBackStackEntryCount() == 0 && !endIfEmpty) loadView(0);
if (fragmentManager.getBackStackEntryCount() == 0 && !userHasLoggedIn)
{
LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(loginBroadcastReceiver);
finish();
}
The method loadView calls is in charge of adding a fragment, based on what the user selected. When it is invoked with 0 as argument, it adds the default fragment.
This the method:
private void loadView(int position)
{
FragmentManager fragmentManager = getSupportFragmentManager();
Bundle fragmentArgs = new Bundle();
if (drawerOpened)
mDrawerLayout.closeDrawer(drawerHolderLayout);
switch (position) {
case 0:
ProfileEditFragment activityList = new ProfileEditFragment();
fragmentArgs.clear();
fragmentArgs.putString(ProfileEditFragment.ARG_AGENT_ID, String.valueOf(prefClass.getAgentId()));
fragmentArgs.putString(ProfileEditFragment.ARG_USERNAME, prefClass.getUsername());
fragmentArgs.putString(ProfileEditFragment.ARG_PASSWORD, prefClass.getHashedPassword());
fragmentArgs.putInt(ProfileEditFragment.ARG_OPERATION, ProfileEditFragment.LOAD_ALL_DATA);
activityList.setArguments(fragmentArgs);
fragmentManager.beginTransaction().add(R.id.main_activity_content, activityList).addToBackStack(null).commit();
//activityList.retrieveActivityFromServer(ProfileEditFragment.LOAD_ALL_DATA);
break;
}
}
The exception occurrs when this line is executed:
fragmentManager.beginTransaction().add(R.id.main_activity_content, activityList).addToBackStack(null).commit();
However, this exception does not occurr when the app is launched for the FIRST TIME. It only appears when it is re-launched after a call to finish(). It is worth mentioning that the first fragment added is a LoginFragment. Once the login process is done, popBackstack is invoked, the backstack becomes empty and loadView(0) is called. So, why does it crash with the exception when the loadView is called but doesn't when LoginFragment is added?
Thanks in advance.