Fragment信息交互之Fragment获取DialogFragment的返回数据

Fragment信息交互会出现使用一个Fragment点击触发DialogFragment,并将信息数据返回到开始的Fragment中的情况。这种情况无法再使用startActivityforResult,所以可以使用setTargetFragment(),以及getTargetFragment().onActivityResult()的方式来进行收发数据。


ContentFragment是依附在ContentActivity上的Fragment,点击ContentFragment中的按钮后,会跳出DialogFragment,此时DialogFragment与ContentFragment是同一个Activity。

Fragment信息交互之Fragment获取DialogFragment的返回数据_第1张图片

关键代码:

1 在ContentFragment设置目标Fragment

EvaluateDialog evaluateDialog = new EvaluateDialog();
				//设置目标Fragment
				evaluateDialog.setTargetFragment(ContentFragment.this, REUEST_CODDE);
				evaluateDialog.show(getFragmentManager(), "evaluate_dialog");
2 在DialogFragment设置返回数据:

	protected void setData(String str) {


		if(getTargetFragment()==null)
		{
			return ;
		}
		Intent intent = new Intent();
		intent.putExtra(REQUESE, EvaluteArg[mWhich]);
		
		//获得目标Fragment,并将数据通过onActivityResult放入到intent中进行传值
		getTargetFragment().onActivityResult(ContentFragment.REUEST_CODDE, Activity.RESULT_OK, intent);
		
	}
3 在ContentFragment对返回数据进行接收解析:

@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==REUEST_CODDE)
		{
			String stringExtra = data.getStringExtra(EvaluateDialog.REQUESE);
			System.out.println("stringExtra :"+stringExtra);
		}
	}

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

ContentFragment:

package com.example.fragmentdialog;

import android.app.Fragment;
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 {
	protected static final int REUEST_CODDE = 0;

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

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View view = inflater.inflate(R.layout.content_fragment, null);
		Button btn = (Button)view.findViewById(R.id.btn_fragment);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				System.out.println("123");
				EvaluateDialog evaluateDialog = new EvaluateDialog();
				//设置目标Fragment
				evaluateDialog.setTargetFragment(ContentFragment.this, REUEST_CODDE);
				evaluateDialog.show(getFragmentManager(), "evaluate_dialog");
			}
		});
		return view;
	}
	
	/**
	 * 接收返回参数
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==REUEST_CODDE)
		{
			String stringExtra = data.getStringExtra(EvaluateDialog.REQUESE);
			System.out.println("stringExtra :"+stringExtra);
		}
	}

}

DialogFragment:

package com.example.fragmentdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class EvaluateDialog extends DialogFragment {

	public String[] EvaluteArg = new String[] { "GOOD", "BAD", "NORMAL" };
	private int mWhich;

	public static final String REQUESE="RESOIBSE";
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		AlertDialog.Builder buidler = new AlertDialog.Builder(getActivity());
		// 直接设置内容模式
		TextView textView = new TextView(getActivity());
		buidler.setView(textView);

		buidler.setItems(EvaluteArg, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				mWhich = which;
				System.out.println("内容 :" + EvaluteArg[which]);
				System.out.println("当前点击项: " + which);
				setData(EvaluteArg[which]);
			}
		});
		
		return buidler.create();
	}

	protected void setData(String str) {


		if(getTargetFragment()==null)
		{
			return ;
		}
		Intent intent = new Intent();
		intent.putExtra(REQUESE, EvaluteArg[mWhich]);
		
		//获得目标Fragment,并将数据通过onActivityResult放入到intent中进行传值
		getTargetFragment().onActivityResult(ContentFragment.REUEST_CODDE, Activity.RESULT_OK, intent);
		
	}
}
ContentActivity:
package com.example.fragmentdialog;

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 ContentActivity extends Activity {

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

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

}




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