View the problem causing the second fragment from FirstFragment

I am doing a simple demo project using Fragmentsin which I call SecondFragmentfrom FirstFragmentto a button.

And I called SecondFragment without any problems, but I am getting view of both the FragmentsSecondFragment and FirstFragment

So where do I make a 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;
        }
    }
}
+4
source share
7 answers

You need to delete the first fragment, you can do this either with replaceor with the first call remove, thenadd

To be able to press the "Back" button, add the transaction to the back stack, you do this by calling addToBackStack(tag)fragments in your manager. The tag may be null.

+2

, Android.

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

, , .

, :

, OnCreate() :

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

.!!

+1

, 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();
                    }
                });
+1

, , rootview scecond

0

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();
    }
0

, . .

I suggest that you make a way to navigate your activities and switch / call between fragments from there.

Below is a small snippet:

In your work with the container:

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 go 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.
0
source
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);
     }
}
0
source

Source: https://habr.com/ru/post/1605681/


All Articles