Some fragments disappear when turning the screen on Android

I have a four fragments, when turning the screen disappears one, if I turn again disappear two and appears the one that was missing in the beginning and finally all disappear except for one, the strangest thing of all is that this only happens in a version of android lower than the 23 or Marshmallow because in version 23 works perfectly, the fragments do not disappear.

I leave some captures and my build.gradle When starting enter the description of the image here

When turning for the first time enter the description of the image here This is my build.gradle

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.alphemsoft.education.regression"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 11
        versionName "1.1"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
//        release {
//            minifyEnabled false
//            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//        }

        release {
            debuggable false
            minifyEnabled true
//            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            debuggable true
            minifyEnabled true
//            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }


    }
}

configurations.all {
    resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.1'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    //    compile 'com.github.bluejamesbond:textjustify-android:+'


    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:design:+'
    compile 'com.android.support:support-v4:+'
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
    compile 'com.android.support:cardview-v7:+'
    compile 'com.codesgood:justifiedtextview:1.0.2'
    compile 'de.psdev.licensesdialog:licensesdialog:1.8.1'
    compile 'com.google.firebase:firebase-ads:10.0.1'
    compile 'rongi.rotate-layout:rotate-layout:2.0.0'
    compile 'io.github.kobakei:ratethisapp:1.1.2'
    compile 'com.github.yavski:fab-speed-dial:1.0.7'
    testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

Thanks pro for your help in advance

This is the code I use to create the first fragment

mBundle=new Bundle();
        mBundle.putBoolean("es_tablet",esTablet);
        main = getFragmentManager().getFragment(inState,"myfragment");
        miFragmentManager.beginTransaction().replace(R.id.content_main, main).commit();

This is the code I use to move from the first fragment to the second

if (botonAceptarPulsado==true){
                mBundle.putBoolean("botonPulsado",botonAceptarPulsado);
//                fragmentDatos.setArguments(mBundle);

                if (fragmentDatos.getArguments()==null){
                    fragmentDatos.setArguments(mBundle);
                }else {
                    getFragmentManager().beginTransaction().detach(fragmentDatos).commit();
                    fragmentDatos.getArguments().putAll(mBundle);
                    getFragmentManager().beginTransaction().attach(fragmentDatos).commit();
                }
                botonAceptarPulsado=false;
            }else{
                mBundle.putBoolean("botonPulsado",botonAceptarPulsado);


//                getFragmentManager().beginTransaction().detach(fragmentDatos);
                if (fragmentDatos.getArguments()==null){
                    fragmentDatos.setArguments(mBundle);
                }else {
                    getFragmentManager().beginTransaction().detach(fragmentDatos).commit();
                    fragmentDatos.getArguments().putAll(mBundle);
                    getFragmentManager().beginTransaction().attach(fragmentDatos).commit();
                    if (tipoDeRegression.equals("lineal")){
                        ((RadioButton)fragmentDatos.getView().findViewById(R.id.rb_lineal)).setChecked(true);
                    }else if (tipoDeRegression.equals("potencial")){
                        ((RadioButton)fragmentDatos.getView().findViewById(R.id.rb_potencial)).setChecked(true);
                    }
                }
            }

To go from second to third

if (getResources().getBoolean(R.bool.es_tablet)==true){
                ((MainFragment)(getFragmentManager().findFragmentByTag("fragment_main"))).llenarListaDeArchivos();

                getFragmentManager().beginTransaction().replace(R.id.content_resultados,resultadosFragmento).addToBackStack(null).commit();

Finally to go from third to last

getFragmentManager().beginTransaction().replace(R.id.content_graficos,graficoFragment).addToBackStack(null).commit();
 2
Author: Mijael Viricochea, 2017-03-05

2 answers

If you are using ViewPager and assuming this is called viewer, set the following

    Visor.setOffscreenPageLimit(max_fragmentos);

Max_fragments = int fixed number of fragments you want to keep in memory and so they will not disappear.

 0
Author: Jorny, 2017-03-14 06:03:06

The problem was that a new instance of the fragment was created and therefore this one was "null" so I made it first check if savedInstanceState was null, if it was it created the fragment, otherwise it doesn't and each Fragment uses

setRetainInstance(true);

And I no longer overwrite the onSaveInstanceState method, my problem was fixed, thank you all for your help.

 0
Author: Mijael Viricochea, 2017-03-15 02:31:31