Oliv. H. Oliv. H. -4 years ago 92
Android Question

Why my fragment still exists on orientation change

I'm learning Android and I'm stuck at "Fragment chapter" because of communication between activity and fragment. Let me introduce my problem :

Got a

MainActivity
where I bound 2 fragments side by side, respectively
AlimentFragment
and
DetailAlimentFragment
using,

On landscape orientation :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="supersk.training.android.gestionstock.AlimentFragment"
android:id="@+id/fgt_aliment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>

<fragment android:name="supersk.training.android.gestionstock.DetailAlimentFragment"
android:id="@+id/fgt_detail"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
</LinearLayout>


On portrait orientation :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fgt_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />


On my
MainActivity
, I defined method which overrides Interface method defined in my
AlimentFragment
class :

@Override
public void onAlimentSelection(Aliment aliment) {
// l'utilisateur selectionne un item dans la liste depuis AlimentFragment
Log.d(MainActivity.class.getSimpleName(), "[onAlimentSelection] method triggered !");

DetailAlimentFragment detailFragment = (DetailAlimentFragment)
getSupportFragmentManager().findFragmentById(R.id.fgt_detail);


if (detailFragment != null ){
// si le fragment est disponible, on est en double vue
detailFragment.updateDetailAliment(aliment);
} else {
// si le fragment n'est pas disponible, on est en simple vue

// on créée le fragment et lui passe l'item selectionné
DetailAlimentFragment newDetailFragment = new DetailAlimentFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_ALIMENT, aliment);
newDetailFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

transaction.replace(R.id.fgt_container, newDetailFragment);
transaction.addToBackStack(null);

// Valider la transaction
transaction.commit();
}
}


User process interaction :


  1. Launch app on portrait (just list displayed from
    AlimentFragment
    )

  2. Click an item and detail item view replaces my list item

  3. Change from
    portrait to landscape (here we can see list at left and detail area
    at right )

  4. Click item and
    DetailAlimentFragment
    correctly updated
    on left

  5. Change from landscape to portrait and see again just a list
    of item Click again on item throws
    NullPointerException



On step 5, the way I found to fix exception is :

if (detailFragment != null && findViewById(R.id.fgt_container) == null) {


Instead of :

if (detailFragment != null ){


Question :

When I change from landscape to portrait, my
DetailAlimentFragment
still remains but why ? Should I call some
DetailActivity
each time I click on item in portrait orientation?

If you need more samples, tell me ! ;)

Answer Source

I finally found some useful helps.

In Android Studio "New project", I can choose the template "Master/detail flow" Activity to generate useful piece of code for mastering it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download