Android Fragment生命周期及其与Activity通信

1.Activity->Fragment

使用Bundle将数据通过setArgument方法传入实例化的Fragment,在Fragment启动时将其传入,在Fragment中调用getArgument方法获得Bundle,再从Bundle中提取数据

                String text = editext.getText().toString();
                MyFragment5 fragment5 = new MyFragment5();
                Bundle bundle = new Bundle();
                bundle.putString("name", text);
                fragment5.setArguments(bundle);
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction beginTransaction = fragmentManager
                        .beginTransaction();
                beginTransaction.add(R.id.layout, fragment5, "fragment5");
                beginTransaction.commit();

2.Fragment->Activity

在自定义Fragment中定义接口,在接口中定义回传数据的函数,将想要回传的数值的数据类型作为参数
Activity继承该接口,并重写回传数据函数,获得Activity所请求的数据并对其操作
在Fragment的OnCreateView方法中调用该回传数据函数,将希望传回Activity的数据作为参数写入回传数据函数,该回传数据函数回调Activity中重写的回传数据函数,对Activity进行操作
——即:在Fragment中定义回调接口,在包含Fragment的Activity中实现接口,Fragment可调用该回调方法将数据回传给Activity

public class MyFragment5 extends Fragment{

    private String code="have received!";
    
    public MyListener listener;
    
    //定义MyListener接口
    public interface MyListener
    {
        public void refunc(String code);
    }
    
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub  
        listener=(MyListener) activity;
        super.onAttach(activity);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment2, container, false);
        TextView tv = (TextView) view.findViewById(R.id.text);
        String text=getArguments().get("name").toString();
        tv.setText(text);
        Toast.makeText(getActivity(), "已成功接收到"+text, Toast.LENGTH_SHORT).show();
        Toast.makeText(getActivity(), "向Activity发送"+code, Toast.LENGTH_SHORT).show();
        //通过接口的方法回调Activity中重写的方法,向Activity传递数值
        listener.refunc(code);
        return view;
    }
}
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main4);
        editext = (EditText) findViewById(R.id.editText);
        send = (Button) findViewById(R.id.send);
        send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String text = editext.getText().toString();
                //通过Bundle将数据传入fragment
                MyFragment5 fragment5 = new MyFragment5();
                Bundle bundle = new Bundle();
                bundle.putString("name", text);
                fragment5.setArguments(bundle);
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction beginTransaction = fragmentManager
                        .beginTransaction();
                beginTransaction.add(R.id.layout, fragment5, "fragment5");
                beginTransaction.commit();
                Toast.makeText(MainActivity4.this, "向Fragment发送数据" + text,
                        Toast.LENGTH_SHORT).show();
            }
        }); 
        
        FragmentManager fragmentManager = getFragmentManager();
        Fragment findFragmentById = fragmentManager.findFragmentById(R.id.frag);
        MyFragment frag=(MyFragment) findFragmentById;
        frag.setAaa("fragment静态传值");
    }

    @Override
    public void refunc(String code) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity4.this, "已成功接收到" + code ,
                Toast.LENGTH_SHORT).show();
    }
}

3.生命周期

4.Fragment事物管理

FragmentManager类:
通过finfFragmentById或findFragmentByTag获取对应的Fragment
调用popBackStack将Fragment从后台栈弹出(模拟用户按下back)

FragmentTransaction类:
添加,删除,替换Fragment。在操作确定后通过commit方法提交事务
可在调用commit之前通过addToBackStack将事务加入back栈,允许用户回退到前一个Fragment状态

你可能感兴趣的:(Android Fragment生命周期及其与Activity通信)