Ejemplo n.º 1
0
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_articles);
    // Check whether the activity is using the layout version with
    // the fragment_container FrameLayout. If so, we must add the first fragment
    if (findViewById(R.id.fragment_container) != null) {

      // However, if we're being restored from a previous state,
      // then we don't need to do anything and should return or else
      // we could end up with overlapping fragments.
      if (savedInstanceState != null) {
        return;
      }

      // Create an instance of ExampleFragment
      HeadlinesFragment firstFragment = new HeadlinesFragment();

      // In case this activity was started with special instructions from an Intent,
      // pass the Intent's extras to the fragment as arguments
      firstFragment.setArguments(getIntent().getExtras());

      // Add the fragment to the 'fragment_container' FrameLayout
      getFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
    }
  }
Ejemplo n.º 2
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_articles);

    // Verificar el tipo de layout que ha dispuesto el MVP
    // Si es el layout-large existirá el contenedor para colocar el contenido del fragmento.
    if (findViewById(R.id.fragment_container) != null) {

      // Si se restaura de otro estado no es necesario cargar el fragmento de nuevo.
      if (savedInstanceState != null) {
        return;
      }

      HeadlinesFragment firstFragment = new HeadlinesFragment();

      // Si la actividad recibe algún parámentro es buena técnica pasarsela a los
      // fragmentos que puedan utilizarlo. Aquí dejamos este ejemplo.
      firstFragment.setArguments(getIntent().getExtras());

      // Ejecutamos la transacción para cargar en el layout-large el fragmento
      getSupportFragmentManager()
          .beginTransaction()
          .add(R.id.fragment_container, firstFragment)
          .commit();
    }
  }