Note: Please note that this issue is happening only for one in thousand users and is not reproducible easily
I have an application in Play store which works fine for most of the users but every now and then I get crash logs related to a peculiar issue.
Can not perform this action after onSaveInstanceState
I assume this happens when I am trying to commit a fragment after onSavedInstanceState()
has been called. But AFAIK, onSavedInstanceState()
will be called after onPause()
and I am doing fragment commit inside onCreate()
of the activity
Here is the code
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.mipmap.arrow_dark_gray);
setSupportActionBar(toolbar);
initFragments();
ActionBar actionBar=getSupportActionBar();
if(actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
//This function is called from onCreate method.
private void initFragments() {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
MyFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(Constants.DATA, customAdapter);
myFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.shopping_container, myFragment);
fragmentTransaction.commit();
}
So clearly I am doing commit()
inside onCreate()
. Is it possible that during onCreate()
onSavedInstanceState()
is called sometimes due to some configuration changes or if the OS is low on memory. Thats why this crash is happening?
And would replacing commit()
with commitAllowingStateLoss()
be the best solution for this problem ?