解决fragement跳转问题

之前做fragement时,在其他界面跳转时总是一个问题,导致不能跳转。

 我想到了一个办法,现在分享出来,还请大牛指点一二。


public class MainActivity extends AppCompatActivity{


    private static FragmentManager fm;
    private static FragmentTransaction fragmentTransaction;

    private static Handler switchFragementHandler=new Handler();

    private  Fragement_Setting mFragement_setting;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fm=getFragmentManager();

        initHandler();
        setDefaultFragement ();
    }

    /**
     * 用于放置fragement跳转的 handler,跳转视图
     */
    public void initHandler() {
        switchFragementHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                fragmentTransaction=fm.beginTransaction(); //开启事务
                switch (msg.what) {
                    case 0:
                        if (mFragement_setting==null) {
                            mFragement_setting = new Fragement_Setting();
                        }
                        fragmentTransaction.replace ( R.id.main_frame_layout,mFragement_setting);
                        break;
                    case 1:
                        //等等fragement
                        break;
                }
                fragmentTransaction.commit();   //事务提交
            }
        };
    }

    /**
     * 设置默认的fragement
     * */
    private void setDefaultFragement(){
        switchFragementHandler.sendEmptyMessage(0);
    }

}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页