Skip to content Skip to sidebar Skip to footer

After Screen Rotation, Findfragmentbyid() Returns A Fragment, Even If There's No Such Id Inside The Layout

I have one layout in two orientations - 1 landscape and 1 portrait. /layout-land/main.xml has two fragments:

Solution 1:

You can check like below : I assume that you have Fragment1 class for the fragment you get by findFragmentById

Fragment1frg1=getFragmentManager().findFragmentById(R.id.FrgDetalleEvento);
booleanhayDetalle= ( frg1!= null && frg1.isInLayout());

It solved the same problem with me, hope it helpful for every of you

Solution 2:

maybe it's too late, but I leave it for future programmers with this problem, I had a similar problem and solved it this way.

Logic: Check the screen orientation, and call findViewById(R.id.fragment2) only if the screen is on landscape mode. Otherwise start a new intent to the other screen

Non working code;

@OverridepublicvoidonEventoSeleccionado(Evento e) {

    booleanhayDetalle= (getFragmentManager().findFragmentById(R.id.FrgDetalleEvento) != null);

            if(hayDetalle) {
                SimpleDateFormatformatter=newSimpleDateFormat("dd/MM/yyyy hh:mm",Locale.ENGLISH);
                Log.i("EOLog", "Hay Detalle");
                ((DetalleEventoFragment)getFragmentManager().findFragmentById(R.id.FrgDetalleEvento)).mostrarDetalle(Long.toString(e.getId()),formatter.format(e.getFecha()));
            }
            else {
                Log.i("EOLog", "No hay detalle");           
                Intenti=newIntent(this,DetalleEventoActivity.class);                                        
                Bundleb=newBundle();
                b.putLong("IDEV", e.getId());           
                b.putLong("FECHA", e.getFecha());           
                i.putExtras(b);
                startActivity(i);
            }

}

Working code:

@OverridepublicvoidonEventoSeleccionado(Evento e) {

      intorientation= getResources().getConfiguration().orientation;

            if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
                SimpleDateFormatformatter=newSimpleDateFormat("dd/MM/yyyy hh:mm",Locale.ENGLISH);
                Log.i("EOLog", "Hay Detalle");
                ((DetalleEventoFragment)getFragmentManager().findFragmentById(R.id.FrgDetalleEvento)).mostrarDetalle(Long.toString(e.getId()),formatter.format(e.getFecha()));
            }
            elseif(orientation == Configuration.ORIENTATION_PORTRAIT) {
                Log.i("EOLog", "No hay detalle");           
                Intenti=newIntent(this,DetalleEventoActivity.class);                                        
                Bundleb=newBundle();
                b.putLong("IDEV", e.getId());           
                b.putLong("FECHA", e.getFecha());           
                i.putExtras(b);
                startActivity(i);
            }else
            {
                Log.i("EOLog", "Dispositivo erroneo");
            }

}

I hope that is helpful

Solution 3:

please cross check the setContentView() of your activity class.

I think it should be- setContentView(R.layout.main);

Post a Comment for "After Screen Rotation, Findfragmentbyid() Returns A Fragment, Even If There's No Such Id Inside The Layout"