I have implemented a formular which has some steps. Every step is in a new Fragment
using a FrameLayout
.
In the last step (FRAGMENT A
) there is a Button
. When this Button
is clicked a new Fragment
(FRAGMENT B
) and the Camera Intent
are launched.
Sometimes after the photo is taken and the Camera is dismissed, the FRAGMENT A
is shown instead of the FRAGMENT B
. When this happens, the UI
elements are frozen, any field is clikable and the only way to continue using the app is closing the Form
and start the process again.
I thought it was a OOM
error so the Activity
gets killed / restored and the last state wasn´t correctly stored.
I've tried to check that but the method onRestoreInstanceState()
is not called. Also methods from the FRAGMENT B
are called after the camera is closed whether it is shown or not. This is the code I called to open the FRAGMENT B
and the camera:
BASE FRAGMENT
private void setCurrentFragment(int pos, boolean animate) {
showFragment(buildFragment(mCurrentPage), animate, animationFromRight);
....
}
private Fragment buildFragment(int pos) {
switch (mHasFamilyManager ? pos : pos + 1) {
............
case 3:
mCurrentFragment = PhotoVerificationFragment.newInstance();
if (mState.getAttachments().isEmpty()) {
switch (mState.getPictureSelector()) {
.....
case CAMERA:
onAddCameraPictureClicked();
break;
}
.....
return mCurrentFragment;
}
private void showFragment(final Fragment fragment, boolean animate,
final boolean animationFromRight) {
if (fragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (animate) {
if (animationFromRight) {
transaction.setCustomAnimations(R.anim.activity_slide_in_right,
R.anim.activity_slide_out_left);
} else {
transaction.setCustomAnimations(R.anim.activity_slide_in_left,
R.anim.activity_slide_out_right);
}
}
transaction.addToBackStack(null);
transaction.replace(R.id.container, fragment);
transaction.commit();
}
}
MainActivity
public void onAddCameraPictureClicked() {
startActivityForResult(takePictureIntent, requestCode);
.....
}
Does anybody have an idea? Thanks in advance!
Please try this:
transaction.add(R.id.container, fragment);
transaction.disallowAddToBackStack();
transaction.commit();
By using replace
you are replacing fragments on top of each other, so it does not remove the previous fragment or it is adding the new fragment below the current one.
Using add
you make sure that it will be always on top.
disallowAddToBackStack
will make sure that you are not holding anything in the stack.