-1

I have multiple fragments in my app,ok lets say i have fragment f1 and f2..now in f1 i have imageview and buttons,now if click on button it goes to f2 and display f2,but in background the view of the f1 is still visible,can any one tell what is the issue with fragments in my app,thanks in advance

Fragment 1 button's listener

Shopfragment is second Fragment

hp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShopFragment tf = new ShopFragment();
                Bundle bundle = new Bundle();
                tf.setArguments(bundle);
                android.app.FragmentManager fm = getActivity().getFragmentManager();
                android.app.FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.container_body, tf);
                ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.addToBackStack(null);
                ft.commit();

            }
        });
Aditya
  • 1,508
  • 1
  • 19
  • 37

5 Answers5

0

Replace it rather than adding it. Something like the following:

FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
FragmentTwo f2 = new FragmentTwo();
ft.replace(R.id.container, f2);
ft.addToBackStack(null);
ft.commit();
Daniel Julio
  • 1,463
  • 3
  • 14
  • 23
0

Maybe you used the two fragment with 2 FrameLayout container, so you replaced the second fragment, the first fragment is not replaced and it's also visible. Check the layout again!

huu duy
  • 2,049
  • 21
  • 31
0

Try to remove old fragment from background:

ShopFragment tf = new ShopFragment();
Bundle bundle = new Bundle();
tf.setArguments(bundle);
android.app.FragmentManager fm = getActivity().getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_body, tf);
                ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

//oldFragment -> reference to old fragment
if(oldFragment != null){
    ft.remove(oldFragment);
}
ft.addToBackStack(null);
ft.commit();
dieter_h
  • 2,707
  • 1
  • 13
  • 19
0

adding a fragment to backstack would cause the old fragment instance not to be destroyed. It's for handling back button clicks by android system to bringing user to old fragment by reversing the transaction.

In your case you should perform your removal operation in another transaction. So that android will not keep the info of this transaction. I should remind that hitting the back button on ft2 will give you activity without a fragment.

    ShopFragment tf = new ShopFragment();
    Bundle bundle = new Bundle();
    tf.setArguments(bundle);
    android.app.FragmentManager fm = getActivity().getFragmentManager();
    android.app.FragmentTransaction ft = fm.beginTransaction();
    ftRemove.remove(oldFragmentInstance);
    ftRemove.commit();

    android.app.FragmentTransaction ftAdd = fm.beginTransaction();
    ftAdd.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ftAdd.add(R.id.container_body, tf);
    ftAdd.addToBackStack(null);
    ftAdd.commit();

would fix your problem hopefully

Deliganli
  • 221
  • 2
  • 10
  • .remove cannot be applied – Aditya Aug 22 '15 at 09:58
  • now click event is not working..i mean nothin is happening on click event – Aditya Aug 22 '15 at 10:01
  • i have edited, there is a new instance named oldFragmentInstance. You should obtain instance of the fragment to be removed using either getFragmentManager().findFragmentById() or getFragmentManager().findFragmentByTag() methods. Putting "this" as oldFragmentInstance parameter would work if you are inside of the fragment – Deliganli Aug 22 '15 at 10:06
  • my app got vanished..:( java.lang.ClassCastException: info.androidhive.materialdesign.activity.DiscoverFragment cannot be cast to android.support.v4.app.Fragment – Aditya Aug 22 '15 at 10:25
  • I don't know the reason you are using android.app.Fragment but i could highly recommend using android.support.v4.app.Fragment instead – Deliganli Aug 22 '15 at 10:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87648/discussion-between-aditya-and-deliganli). – Aditya Aug 22 '15 at 10:46
0

Just ensure whether you used

android.support.v4.app.Fragment

for first Fragment placement.If you done like that then the problem is with this one. when you try to place the Fragment with different support version you will receive the problem.

(i.e)if you place

android.app.Fragment

support version fragment above the

android.support.v4.app.fragment

support version fragment.you will get the android.app.Fragment support version fragment as transparent.

In order to solve this problem use same support version fragment.

INFO ABOUT TWO SUPPORT VERSIONS

android.support.v4.app.Fragment

android.support.v4.app.Fragment is the Fragment class in the android support library, which is a compatibility package that allows you to use some of the newer features of Android on older versions of Android.

android.app.Fragment

android.app.Fragment is the Fragment class in the native version of the Android SDK. It was introduced in Android 3 (API 11).

If you want to make your app use fragments, and want to target devices before API 11, you must use android.support.v4.app.Fragment. However, if you're only targeting devices running API 11 or above, you can use android.app.Fragment.

Vishwa
  • 1,112
  • 1
  • 11
  • 23