Some fragments disappear when you rotate the screen in Android

2

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

I leave some catch and my build.gradle At the beginning

When turning for the first time 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'

Thank you 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 go from the first fragment to the second one

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 the second to the 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();
    
asked by Mijael Viricochea 05.03.2017 в 05:36
source

2 answers

0

The problem was that a new instance of the fragment was created and therefore this was "null" so I had it first check if savedInstanceState was null, if it was it created the fragment, otherwise it does not and every Fragment uses

setRetainInstance(true);

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

    
answered by 15.03.2017 / 03:31
source
0

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

    Visor.setOffscreenPageLimit(max_fragmentos);

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

    
answered by 14.03.2017 в 07:03