本文介绍了查看问题从FirstFragment调用SecondFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用片段,其中我打电话做一个简单的演示项目 SecondFragment FirstFragment 按钮点击。

和我打电话SecondFragment没有任何问题,但我获得视图两个片段的 SecondFragment和FirstFragment

那么,我做错误?

 公共类MainActivity扩展AppCompatActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        如果(savedInstanceState == NULL){
            getSupportFragmentManager()的BeginTransaction()
                    。新增(R.id.container,新FirstFragment())提交()。
        }
    }

    公共静态类FirstFragment扩展片段{

        按钮buttonCallSecondFragment;

        公共FirstFragment(){
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_first,集装箱,
                    假);

            buttonCallSecondFragment =(按钮)rootView.findViewById(R.id.button1);
            buttonCallSecondFragment.setOnClickListener(新OnClickListener(){

                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根
                    FragmentManager FM = getFragmentManager();
                    SecondFragment片段=新SecondFragment();
                    FragmentTransaction英尺= fm.beginTransaction();
                    ft.add(R.id.container,片段);
                    ft.commit();
                }
            });

            返回rootView;
        }
    }


    公共静态类SecondFragment扩展片段{

        公共SecondFragment(){
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_second,集装箱,
                    假);
            返回rootView;
        }
    }
}
 

解决方案

您需要删除的第一个片段,你可以做,通过使用替换或首先调用删除然后添加

要能够preSS后退按钮事务添加到后退堆栈,你这样做,通过调用 addToBackStack(标签)您片段经理。标签可以为空。

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;
        }
    }
}
解决方案

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.

这篇关于查看问题从FirstFragment调用SecondFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:51