android Activity和Fragment之间的数据传输的实现

Activity传递给Fragment的数据 可以附加argument给Fragment来实现,需要调用Fragment.setArguments(Bundle)方法。注意,该任务必须在Fragment创建后、添加给Activity前完成。

Fragment传递给Activity的数据,可以再Fragment中定义一个内部接口,然后由Activity继承实现,然后在Fragment中获取这个接口的实例,就可以根据接口调用Activity的内部方法了。

由于在android的实现机制中fragment和activity会被分别实例化为两个不相干的对象,他们之间的联系由activity的一个成员对象fragmentmanager来维护.fragment实例化后会到activity中的fragmentmanager去注册一下,这个动作封装在fragment对象的onAttach中,所以你可以在fragment中声明一些回调接口,当fragment调用onAttach时,将这些回调接口实例化,这样fragment就能调用各个activity的成员函数了,当然activity必须implements这些接口,

实现代码如下:

Activity的布局代码:



    
    

Activity的Java代码:

public class C_Activity extends FragmentActivity implements MyListener{

	public Button bt;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c_activity);
		findView();
		setFragment();
	}

	private void findView() {
		// TODO Auto-generated method stub
		bt = (Button) findViewById(R.id.bt);
	}


	//启动Fragment 并且将当前时间作为数据传给Fragment
	private void setFragment() {
		// TODO Auto-generated method stub
		FragmentManager fm = getSupportFragmentManager();
		MyData data = new MyData();
		data.setContent(System.currentTimeMillis()+"");
		
		fm.beginTransaction().add(R.id.a_fragment, MyFragment.newInstance(data)).commit();
		
	}

	//从Fragment获取数据 ,并且在Button上显示出来
	@Override
	public void setData(MyData data) {
		// TODO Auto-generated method stub
		bt.setText(data.getContent());
	}

}

Fragment的布局代码:



    
    

Fragment的Java代码:

public  class MyFragment extends Fragment{
	
	public static String key = "MyFragment.key";
	public static int requestCode = 0;
	
	public MyData mydata;
	public Button bt;
	public MyListener mylistener;
	
	
	
	//activiy必须实现这个接口
	public interface MyListener{
		public void setData(MyData data);
	} 
	
	//Fragment第一次附属于Activity时调用,在onCreate之前调用 
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mylistener = (MyListener) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		GetData();
		
	}

	//Activity传过来的数据
	private void GetData() {
		// TODO Auto-generated method stub
		mydata = (MyData) getArguments().getSerializable(key);
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.a_fragment_layout, container,false);
		bt = (Button) view.findViewById(R.id.bt);
		bt.setText(mydata.getContent());
		setListener();
		
		return view;
	}
	
	//将数据发送给Activity
	private void setListener() {
		// TODO Auto-generated method stub
		bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				MyData data = new MyData();
				data.setContent(System.currentTimeMillis()+"");
				mylistener.setData(data);
			}
		});
		
	}

	public static MyFragment newInstance(MyData data)
	{
		Bundle bundle = new Bundle();
		bundle.putSerializable(key, data);
		
		MyFragment mf = new MyFragment();
		mf.setArguments(bundle);
		
		return mf;
	}
	
}



你可能感兴趣的:(Java,Android)