Oreo
Oreo

Reputation: 2594

View Issue Calling SecondFragment from FirstFragment

I am making a simple demo project using Fragments, in which i am calling SecondFragment from FirstFragment on button click.

And i called SecondFragment without any issue, but i getting view of both the Fragments SecondFragment and FirstFragment

So where i am doing mistake ?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new FirstFragment()).commit();
        }
    }

    public static class FirstFragment extends Fragment {

        Button buttonCallSecondFragment;

        public FirstFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_first, container,
                    false);

            buttonCallSecondFragment = (Button) rootView.findViewById(R.id.button1);
            buttonCallSecondFragment.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    FragmentManager fm = getFragmentManager();
                    SecondFragment fragment = new SecondFragment();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.add(R.id.container, fragment);
                    ft.commit();
                }
            });

            return rootView;
        }
    }


    public static class SecondFragment extends Fragment {

        public SecondFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_second, container,
                    false);
            return rootView;
        }
    }
}

Upvotes: 4

Views: 87

Answers (7)

Patel Hiren
Patel Hiren

Reputation: 305

private void selectItem(int position) {
    selectedPosition = position;
    // update the main content by replacing fragments
    fragment = null;
    fragmentStack = new Stack<Fragment>();
     switch (position) {

         case 0:

             fragment = new  Fragment1();

             break;

         case 1:

             fragment = new  Fragment2();


             break;

    }
     //redirect the screen
     if (fragment != null) {
        fragmentManager = getSupportFragmentManager();
        redirectScreen(fragment);
        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(title[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
     }
}

Upvotes: 0

koutuk
koutuk

Reputation: 832

if you want to replace fragment you should call replace in place of add :

buttonCallSecondFragment = (Button) rootView.findViewById(R.id.button1);
                buttonCallSecondFragment.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        FragmentManager fm = getFragmentManager();
                        SecondFragment fragment = new SecondFragment();
                        FragmentTransaction ft = fm.beginTransaction();
                        ft.replace(R.id.container, fragment);
                        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                        ft.addToBackStack(null);
                        ft.commit();
                    }
                });

Upvotes: 1

Kaveesh Kanwal
Kaveesh Kanwal

Reputation: 1763

No, you cannot communicate between fragments like this. You need to communicate between fragments via the container activity.

I suggest, you make a navigation method in your activity and switch/call between fragments from there.

Below is a small snippet:

In your container activity:

Fragment fragment;
    public void navigateTo(Fragment newFragment,boolean addToBackStack) {

        this.fragment = newFragment;
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.fragment_container, newFragment);
        if(addToBackStack)
            ft.addToBackStack(fragment.getClass().getSimpleName());
        ft.commit();
    }

If you are going from Fragment1 to Fragment2 then in your Fragment1 do the following:

((YourContainerActivity) getActivity()).navigateTo(new Fragment2(),false); //if you want to add to back stack make 2nd argument true.

Upvotes: 0

LOG_TAG
LOG_TAG

Reputation: 20569

you can also load that as Nested fragment

public void addFragB() {
        FragmentManager childFragMan = getChildFragmentManager();
        FragmentTransaction childFragTrans =    childFragMan.beginTransaction();
        SecondFragment 2ndfrag = new SecondFragment();
        childFragTrans.add(R.id.fragA_LinearLayout, 2ndfrag);
        //childFragTrans.addToBackStack("");
        childFragTrans.commit();
    }

Upvotes: 0

Narendra Kothamire
Narendra Kothamire

Reputation: 973

You are adding the fragment so it will come on top of another fragment give background color to rootview of scecond fragment

Upvotes: 0

AndiGeeky
AndiGeeky

Reputation: 11474

You are adding fragment on already displaying fragment in your android app.

 FragmentTransaction ft = fm.beginTransaction();
                    ft.add(R.id.container, fragment);
                    ft.commit();

Do not add fragment but replace fragment when already one fragment is loaded on activity.

So for implementing that :

Please add your code in OnCreate() and add below code to your click listener :

 FragmentManager fm = getFragmentManager();
                    SecondFragment fragment = new SecondFragment();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.container, fragment);
                    ft.addToBackStack(null);
                    ft.commit();

Thank you.!!

Upvotes: 1

JohanShogun
JohanShogun

Reputation: 2976

You need to remove the first fragment, you can do that either by usingreplace or first calling remove then add

To be able to press the back button add the transaction to the back stack,you do that by calling addToBackStack(tag) on your fragment manager. Tag may be null.

Upvotes: 2

Related Questions