Madhu
Madhu

Reputation: 73

Fragment hide not working in Android

My requirement is, I've a main activity and I want to display an image and text instead of progress bar in a separate fragment. I've a separate fragment and I want to display the fragment when user clicks on a button in MainActivity.

My issue is, when I start the app, the fragment gets displayed by default. I want the fragment to be displayed only when user clicks on button. When I start the app I want the activity to be displayed.

I've an activity_main xml as below

<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"
    android:background="@drawable/background" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="*" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dip" >

                    <TextView
                        android:id="@+id/text1"
                        style="@style/textForLabel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dip"
                        android:text="@string/text1" />

                    <Spinner
                        android:id="@+id/spinner1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="20dip"
                        android:layout_weight="1"
                        android:drawSelectorOnTop="true"
                        android:prompt="@string/spinner1"
                        android:spinnerMode="dialog" >
                    </Spinner>
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/text2"
                        style="@style/textForLabel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dip"
                        android:text="@string/label2" />

                    <Spinner
                        android:id="@+id/spinner2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="20dip"
                        android:layout_span="6"
                        android:layout_weight="1"
                        android:drawSelectorOnTop="true"
                        android:prompt="@string/spinner2"
                        android:spinnerMode="dropdown" />
                </TableRow>
            </TableLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/text3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="@color/red"
                    android:textSize="@dimen/text_medium"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <fragment
        android:id="@+id/progress_bar_fragment"
        android:name="com.test.pushnotificationeclipse.ProgressBarFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

The fragemnt_progress_bar.xml is as below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/NoActionBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_all" >

    <ImageView
        android:id="@+id/imageView_frag_pgbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text_frag_pgbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView_frag_pgbar"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="74dp"
        android:textColor="@color/basic_color"
        android:textSize="@dimen/text_medium_large" />

</RelativeLayout>

My ProgressBarFragment.java is as below

public class ProgressBarFragment extends Fragment {

    @InjectView(R.id.text_frag_pgbar)
    TextView textView;

    @InjectView(R.id.imageView_frag_pgbar)
    ImageView imageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_progress_bar, container,
                false);
        ButterKnife.inject(this, view);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}

My MainActivity.java is as below: When I pass true tohideAndShowFragment(), the fragment should be hidden and when I pass false the fragment should be shown.

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        context = getApplicationContext();
        hideAndShowFragment(true);
    }

    public void hideAndShowFragment(boolean hide) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ProgressBarFragment pf = new ProgressBarFragment();
        if (hide) {
            ft.hide(pf).commit();
        } else {
            ft.show(pf).commit();
        }
    }
}

Upvotes: 2

Views: 8195

Answers (5)

Shahid Hunzai
Shahid Hunzai

Reputation: 1

          int count =1;
           if (count == 1) {


               FragmentManager fm = getFragmentManager();
               FragmentTransaction fragmentTransaction = 
               fm.beginTransaction();
               fragmentTransaction.replace(R.id.labelframe, new 
                labelfregaments());
               fragmentTransaction.commit(); 
               count =0;
           }
           else if (count == 0) {



               FragmentManager fm = getFragmentManager();
               labelfregaments fs = (labelfregaments)fm.findFragmentById(R.id.labelframe);
               FragmentTransaction fragmentTransaction = fm.beginTransaction();
               fragmentTransaction.hide(fs);
               fragmentTransaction.commit();


               count=1;

           }

Upvotes: 0

Madhu
Madhu

Reputation: 73

I resolved it by adding the fragment instead of having it shown:

public void hideAndShowFragment(boolean hide) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ProgressBarFragment pf = (ProgressBarFragment) fm.findFragmentById(R.id.progress_bar_fragment);
        if (hide) {
            ft.hide(pf).commit();
        } else {
ft.add(android.R.id.content, pf, "ProgressFragmentTag").commit();
        }
    }

Upvotes: 0

Andy B
Andy B

Reputation: 753

You might be better of getting an instance of the fragment in code and using the technique from the official docs as shown below and here (search for 'newInstance'). Add the following static method to your fragment.

public static ProgressFragment newInstance() {
    DetailsFragment f = new DetailsFragment();

    // Supply args here if needed.
    /*Bundle args = new Bundle();
    args.putInt("index", index);
    f.setArguments(args);*/

    return f;
}

Once you have that you can add the fragment to the top view layout with the following fragment transaction code which is inside the button click method.

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.add(android.R.id.content, ProgressFragment.newInstance(), "ProgressFragmentTag");
tr.commit();

You can then use the following code to remove the fragment if needed;

FragmentManager fm = getSupportFragmentManager();
ProgressFragment frag = fm.findFragmentByTag("ProgressFragmentTag")
if (frag!=null)
{
   FragmentTransaction tr = fm.beginTransaction();
   tr.remove(frag).commit();
}

Hope this helps!

Upvotes: 0

jacobhyphenated
jacobhyphenated

Reputation: 2145

The issue here is your hideAndShowFragment method. You are creating a new instance of ProgressBarFragment, but the FragmentTransaction hide and show methods only work on an attached fragment.

Instead, you should get the fragment you already defined in your XML by id.

public void hideAndShowFragment(boolean hide) {
    FragmentManager fm = getFragmentManager();
    ProgressBarFragment pf = 
        (ProgressBarFragment) fm.findFragmentById(R.id.progress_bar_fragment);
    FragmentTransaction ft = fm.beginTransaction();
    if (hide) {
        ft.hide(pf).commit();
    } else {
        ft.show(pf).commit();
    }
}

Additionally, this line is concerning:

context = getApplicationContext();

Your Activity is also a Context object, so unless you need the application specific context for something, you generally want to be using this where you need a context inside your activity.

Upvotes: 4

squiroid
squiroid

Reputation: 14027

 FragmentManager fm = getFragmentManager();
    fm.beginTransaction()
              .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
              .show(yourfragment)
              .commit();

Ps:-Dont Don't mess with the visibility flags of the container - FragmentTransaction.hide/show does that internally for you.

Src:- "https://stackoverflow.com/a/16490344/1632286"

Upvotes: 0

Related Questions