My app shows a lot of images on the main screen. The user can view more information about a product by touching an image. The main screen fragment gets hidden and the product detail fragment becomes visible. By clicking the back key, the main screen fragment becomse visible again.
The fragment transacion is implemented as follows:
@Override
public void showProduct(Product p, boolean isParentTabbed) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
// the new fragment
Fragment mFragment = new ProductDetailFragment(p,isParentTabbed);
//hide main screen fragment and add product detail fragment
transaction.hide(currentlyOpenedFragment);
transaction.add(android.R.id.content,mFragment);
//set new fragment as current "on top" fragment
currentlyOpenedFragment = mFragment;
//start animation
transaction.setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_top);
transaction.addToBackStack(null);
transaction.commit();
}
Everything works fine, except if the user openes the share dialog (standard android share intent) in the product detail fragment and closes the dialog by clicking the back key. For some reason, the onResume method in the main screen fragment (which is hidden) is called. I solved the problem by adding the following code to the onResume method in the main screen fragment:
super.onResume();
if(this.isHidden()){
Log.d("tab","dont resume tab0fragment because it is hidden");
return;
}
This works fine, but the question remains: Why is onResume() called in the hidden fragment when the user closes a share dialog in an other fragment?
Hidden fragments still follow the fragment lifecycle. Take a look at the flowchart from the documentation. User navigates backwards or the fragment is removed/replaced.
Leads to onDestroyView()
being called, where The fragment returns to the layout from the back stack,
which is where your main screen fragment is sitting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With