After the screen is rotated, findFragmentById () returns a fragment, even if there is no such identifier in the layout

I have one layout in two orientations - 1 landscape and 1 portrait.

/layout-land/main.xml has two fragments:

  • <fragment android:id="@+id/fragment1" .. />
  • <fragment android:id="@+id/fragment2" .. />

/layout/main.xml only has one snippet:

  • <fragment android:id="@+id/fragment1" .. />

Here : MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    firstFragment = (FirstFragment) getFragmentManager().findFragmentById(R.id.fragment1);
    secondFragment = (SecondFragment) getFragmentManager().findFragmentById(R.id.fragment2);
}

Then I launched the mode MainActivity.javain the landscape . In this case

  • Both firstFragmentand secondFragmentare fragments in the layout layout-land/main.xml

Then I rotate the screen to portrait mode and the layout file layout/main.xmlshould be loaded. In this case

  • firstFragment refers to R.id.fragment1
  • secondFragmentrefers to a nonexistent fragment. Access to any elements inside this object throws a NullPointerException. (To be more precise, notsecondFragment here ) null

secondFragment , ?


: , Android http://developer.android.com/training/basics/fragments/creating.html:

, , , , . , . , , false isInLayout(). ( , , .)

+4
3

, , , .

: findViewById(R.id.fragment2), .

;

@Override
public void onEventoSeleccionado(Evento e) {

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

            if(hayDetalle) {
                SimpleDateFormat formatter = new SimpleDateFormat("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");           
                Intent i = new Intent(this,DetalleEventoActivity.class);                                        
                Bundle b = new Bundle();
                b.putLong("IDEV", e.getId());           
                b.putLong("FECHA", e.getFecha());           
                i.putExtras(b);
                startActivity(i);
            }

}

:

@Override
public void onEventoSeleccionado(Evento e) {

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

            if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
                SimpleDateFormat formatter = new SimpleDateFormat("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 if(orientation == Configuration.ORIENTATION_PORTRAIT) {
                Log.i("EOLog", "No hay detalle");           
                Intent i = new Intent(this,DetalleEventoActivity.class);                                        
                Bundle b = new Bundle();
                b.putLong("IDEV", e.getId());           
                b.putLong("FECHA", e.getFecha());           
                i.putExtras(b);
                startActivity(i);
            }else
            {
                Log.i("EOLog", "Dispositivo erroneo");
            }

}

,

+2

, : , Fragment1 , findFragmentById

Fragment1 frg1 =getFragmentManager().findFragmentById(R.id.FrgDetalleEvento);
boolean hayDetalle = ( frg1!= null && frg1.isInLayout());

, ,

+3

, setContentView() .

, setContentView (R.layout.main);

0

All Articles