Fragment信息交互之不同Activity获取信息方式

应用场景1:(两个Fragment分别贴在两个不同的Activity上,即用一个Fragment上打开另一个Fragment。)

第一种方式:

在ListTitleFragment中利用startActivityForResult去启动另一个贴附Activity的ContentFragment,在ContentFragment中使用setResult去回传参数,并在ListTitleFragment中利用onActivityResult获取参数。

note:这种方式需要在使用setResult之后,将ContentFragment关闭(finish())才能回传成功参数。

Fragment信息交互之不同Activity获取信息方式_第1张图片

ListTitleFragment:

package com.example.fragment1.fragment;

import java.util.ArrayList;

import com.example.fragment1.R;
import com.example.fragment1.R.id;
import com.example.fragment1.R.layout;
import com.example.fragment1.activity.ContentActivity;

import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 在ListTitleFragment中是一个列表Fragment
 * 点击Fragment中Item时,建立新的Activity(ContentActivity
 * ),同时ContentFragment也依附在ContentActivity上
 * 可以在ListTitleFragment中使用startActivityForResult来获取ContentActivity的数据;
 * 
 * ** 
 * 1 startActivityForResult(intent, RESQUSE_CODE); 
 * 2 在ContentActivity的ContentFragment中getActivity
 * ().setResult(ListTitleFragment.RESQUSE_CODE, intent); 3 onActivityResult获取返回值
 * 
 * @author admin
 * 
 */
public class ListTitleFragment extends ListFragment {

	private ArrayList<String> arraylist;

	public static final int RESQUSE_CODE = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		InitData();
		myListAdapter myListAdapter = new myListAdapter(arraylist);
		setListAdapter(myListAdapter);

	}

	private void InitData() {
		arraylist = new ArrayList<String>();
		arraylist.add("123");
		arraylist.add("234");
		arraylist.add("345");
		arraylist.add("456");
		arraylist.add("567");
		arraylist.add("678");
		arraylist.add("789");
	}

	public class myListAdapter extends ArrayAdapter<String> {

		public myListAdapter(ArrayList<String> list) {
			super(getActivity(), 0, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(
						R.layout.activity_list_item, null);
			}
			TextView textview = (TextView) convertView
					.findViewById(R.id.tvtext);
			textview.setText(arraylist.get(position));
			return convertView;
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);

		/**
		 * 使用startActivityForResult方式请求参数返回。
		 */
		Intent intent = new Intent(getActivity(), ContentActivity.class);
		startActivityForResult(intent, RESQUSE_CODE);

	}

	/**
	 * 获取回传的参数
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == RESQUSE_CODE) {
			// 获取返回值
			String str = (String) data.getExtras().get("response");
			System.out.println(str);
		}

	}

}

ContentFragment:

package com.example.fragment1.fragment;

import com.example.fragment1.R;
import com.example.fragment1.R.id;
import com.example.fragment1.R.layout;

import android.app.Fragment;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class ContentFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_content_litem, null);
		Button BtnContent = (Button) view.findViewById(R.id.tv_content_item);
		BtnContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				System.out.println("123");
				Intent intent = new Intent();
				intent.putExtra("response", "good");
				/**
				 * 使用setResult将参数设置给启动ContentFragment的Fragment
				 */
				getActivity().setResult(ListTitleFragment.RESQUSE_CODE, intent);

				getActivity().finish();
			}
		});
		return view;
	}

}


两个Fragment依附的Activity:

ContentActivity:

package com.example.fragment1.activity;

import com.example.fragment1.R;
import com.example.fragment1.R.id;
import com.example.fragment1.R.layout;
import com.example.fragment1.fragment.ContentFragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class ContentActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_content);
		InitContentFragment();
	}

	private void InitContentFragment() {

		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		ContentFragment contentFragment = new ContentFragment();
		transaction.replace(R.id.fl_content, contentFragment);
		transaction.commit();
	}
}


ListFragmentActivity:

package com.example.fragment1.activity;

import com.example.fragment1.R;
import com.example.fragment1.R.id;
import com.example.fragment1.R.layout;
import com.example.fragment1.fragment.ListTitleFragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class ListFragmentActivity extends Activity {

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

	private void InitListTitleFragment() {
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		ListTitleFragment listTitleFragment = new ListTitleFragment();
		transaction.replace(R.id.fldetail, listTitleFragment);
		transaction.commit();

	}

}


本例代码:http://download.csdn.net/detail/xiaoleiacm/9481778

你可能感兴趣的:(android,Fragment)