kaneda
kaneda

Reputation: 6187

Strange Fragment behavior

I have the following code on my onResume() method of my Activity:

mFragmentManager.popBackStack(FragmentNames.FRAG_MY_CLASS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction fragTransaction = mFragmentManager.beginTransaction();
fragTransaction.add(R.screenHome.content, MyClass.newInstance(), FragmentNames.FRAG_MY_CLASS);
fragTransaction.commit();

It works as intended. The behavior I expected was to pop from the backstack the specified fragment if before adding it. I do this because I wanted to recreate the fragment view everytime, when I come to it from the previous activity or when I press back from the next activity. But I don't understand why it works, popping up the specified fragment if I didn't do this before commiting:

fragTransaction.addToBackStack(FragmentNames.FRAG_MY_CLASS);

Can anyone know why it works? Oddly, also is that I call popBackStack with the tag name I used to add the specified Fragment.

Upvotes: 0

Views: 88

Answers (2)

Anderson K
Anderson K

Reputation: 5505

You can keep creating the fragment in method onCreate (), and the fragment control the view that should be displayed according to the connection status

public class FragmentA extends Fragment {

    boolean      flagNetwork;
    LinearLayout myContent;

    LinearLayout networkError;

    @Override
    public View onCreateView( final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState ) {

        final View view = inflater.inflate( R.layout.fragment_main, null );

        this.myContent = ( LinearLayout ) view.findViewById( R.id.content );
        this.networkError = ( LinearLayout ) view.findViewById( R.id.network_unvailable );

        if ( this.flagNetwork ) {
            this.myContent.setVisibility( View.VISIBLE );
        } else {
            this.networkError.setVisibility( View.VISIBLE );
        }

        return view;
    }
}

//Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:visibility="gone" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/network_unvailable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:visibility="gone" >

        <!-- your hadler to error -->

    </LinearLayout>

</RelativeLayout>

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

While not directly answering your question, you stated that you do all this because

I wanted to recreate the fragment view everytime

which is kinda wrong approach. You should recreate your fragments's view in its onResume() - no need to touch backstack for this, especially you are doing this when

I come to it from the previous activity

which perfectly fits.

Upvotes: 1

Related Questions