I have this navigation schema in my android application:
PrimaryActivity (... ListView onClick - sending <PrimaryUri> via intent...) >
MasterActivity (TabLayout, ViewPager, FragmentPageAdapter) >
FragmentA / FragmentB (...ListView onClick - sending <PrimaryUri> + <DetailUri> via intent...) > DetailActivity.
Now when I press BACK button in DetailActivity, MasterActivity gets destroyed and recreated again with new instances of Fragment A and B and Intent data (<PrimaryUri>) is lost.
I tried persisting <PrimaryUri> between Activies by this way:
Now I really appreciate help for question: How to persist <PrimaryUri> in my application? I prefer to use intents and solve this strange behaviour. Some forum discussed errors in FragmentPageAdapter etc... Maybe I will refactor it for use of SharedPreferences if it will be quick solution...
EDIT: Some additional info:
In DetailActivity:
@Override
public void finish() {
Intent resultIntent = new Intent();
resultIntent.setData((Uri) getIntent().getParcelableExtra(FragmentB.MY_URI));
setResult(Activity.RESULT_OK, resultIntent);
super.finish();
}
In MasterActivity I have:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
In FragmentB I have:
public void onItemSelected(Uri contentUri) {
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra(PRIMARY_URI, mUri);...
startActivityForResult(intent, FragmentB.BACKTAG);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (FragmentB.BACKTAG): {
if (resultCode == Activity.RESULT_OK) {
mUri = data.get...
}
}
break;
}
}
The strange behavior looks like:
I think that reason is that onDestroy appears while waiting for onActivityResult. But why it is called and why the MasterActivity isn't fully recreated to catch onActivityResult callback?
This was headache, but here I found the solution after 2 days of futile attempts. This has nothing to do with FragmentPageAdapter. I am lucky woman!