Android Fragment的使用 七 Argument和startActivityForResult传递数据

来源:互联网 发布:mac dare you试色视频 编辑:程序博客网 时间:2024/06/06 18:15

Activity向Fragment的传递信息,我们都是通过getIntent.getExtra这样获得的,
然而这样有弊端,因为这个需要跳转activity,可以说是很麻烦。
然而Fragment有Argument参数能够设置,使用这个Fragment的activity创建Fragment后能够设置参数,然后Fragment接受传输数据。
使用例子如下:

public class MainActivity extends Activity{   @Override   protected void onCreate(Bundle savedInstanceState)   {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);   }   public void showLoginDialog(View view)   {      FragmentManager fragmentManager = getFragmentManager();      EditNameDialogFragment newFragment = EditNameDialogFragment.newInstance("呵呵");         // The device is smaller, so show the fragment fullscreen      FragmentTransaction transaction = fragmentManager               .beginTransaction();         // For a little polish, specify a transition animation      transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);      transaction.replace(R.id.id_ly, newFragment).commit();   }}public class EditNameDialogFragment extends Fragment{   private String mArgument;   public static final String ARGUMENT = "argument";   @Override   public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState)   {      View view = inflater.inflate(R.layout.fragment_edit_name, container,            false);      Bundle bundle = getArguments();      if (bundle != null)      {         mArgument = bundle.getString(ARGUMENT);         Log.v("zzw",mArgument);      }      return view;   }   public static EditNameDialogFragment newInstance(String argument)   {      Bundle bundle = new Bundle();      bundle.putString(ARGUMENT, argument);      EditNameDialogFragment contentFragment = new EditNameDialogFragment();      contentFragment.setArguments(bundle);      return contentFragment;   }}activity_main<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:onClick="showLoginDialog"    android:text="自动适应屏幕" />    <FrameLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/id_ly"        ></FrameLayout></LinearLayout>fragment_edit_name<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content" >    <TextView        android:id="@+id/id_label_your_name"        android:layout_width="wrap_content"        android:layout_height="32dp"        android:gravity="center_vertical"        android:text="Your name:" />    <EditText        android:id="@+id/id_txt_your_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/id_label_your_name"        android:imeOptions="actionDone"        android:inputType="text" />    <Button        android:id="@+id/id_sure_edit_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@id/id_txt_your_name"        android:text="ok"/>    <Button        android:id="@+id/id_cancel_edit_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/id_txt_your_name"        android:text="cancel"        android:layout_alignParentLeft="true"/></RelativeLayout>

接下来我在给大家讲一下子activity的startActivityForResult
如何在Fragment里使用,虽然Fragment能够直接使用startActivityForResult跳转activity,但是如果需要对方activity的Fragment接受数据,这需要一个中转过程,由宿主activity接受数据,然后通过Argument传送数据给Fragment,然后由Fragment返回数据。

使用例子如下,(MainActivity和EditNameDialogFragment
的布局代码如上一样就不重复贴了):

public class MainActivity extends Activity{   @Override   protected void onCreate(Bundle savedInstanceState)   {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);   }   public void showLoginDialog(View view)   {      FragmentManager fragmentManager = getFragmentManager();      EditNameDialogFragment newFragment = new EditNameDialogFragment();         // The device is smaller, so show the fragment fullscreen      FragmentTransaction transaction = fragmentManager               .beginTransaction();         // For a little polish, specify a transition animation      transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);      transaction.replace(R.id.id_ly, newFragment).commit();   }}public class EditNameDialogFragment extends Fragment{   public static final int REQUEST_DETAIL = 0x110;   public static final String ARGUMENT = "argument";   public static final String RESPONSE = "response";   @Override   public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState)   {      View view = inflater.inflate(R.layout.fragment_edit_name, container,            false);        view.findViewById(R.id.id_sure_edit_name).setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {            Intent intent = new Intent(getActivity(),ContentActivity.class);            intent.putExtra(ARGUMENT,"来自EditNameDialogFragment的消息");            startActivityForResult(intent, 0);         }      });      return view;   }   @Override   public void onActivityResult(int requestCode, int resultCode, Intent data)   {   super.onActivityResult(requestCode, resultCode, data);   if(requestCode==0 && resultCode==REQUEST_DETAIL) {      Log.e("zzw", "receive");      }   }}public class ContentFragment extends Fragment{   public static final int REQUEST_DETAIL = 0x110;   public static final String ARGUMENT = "argument";   public static final String RESPONSE = "response";   @Override   public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState)   {      View view = inflater.inflate(R.layout.fragment_login_dialog, container,            false);      Bundle bundle = getArguments();      if (bundle != null) {         String mArgument = bundle.getString(ARGUMENT);         Log.v("zzw", mArgument);         Intent intent = new Intent();         intent.putExtra(RESPONSE, "good");         getActivity().setResult(REQUEST_DETAIL, intent);         Log.v("zzw", "reponse");      }      return view;   }   public static ContentFragment newInstance(String argument)   {      Bundle bundle = new Bundle();      bundle.putString(ARGUMENT, argument);      ContentFragment contentFragment = new ContentFragment();      contentFragment.setArguments(bundle);      return contentFragment;   }}public class ContentActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.content);        String title = getIntent().getStringExtra(ContentFragment.ARGUMENT);        FragmentManager fragmentManager = getFragmentManager();        ContentFragment newFragment = ContentFragment.newInstance(title);        // The device is smaller, so show the fragment fullscreen        FragmentTransaction transaction = fragmentManager                .beginTransaction();        // For a little polish, specify a transition animation        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);        transaction.replace(R.id.id_lycontent, newFragment).commit();    }}content<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <FrameLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/id_lycontent"        ></FrameLayout></LinearLayout>fragment_login_dialog<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical" >    <ImageView        android:layout_width="match_parent"        android:layout_height="64dp"        android:background="#FFFFBB33"        android:contentDescription="@string/app_name"        android:scaleType="center"        android:src="@drawable/title" />    <EditText        android:id="@+id/id_txt_username"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="4dp"        android:layout_marginLeft="4dp"        android:layout_marginRight="4dp"        android:layout_marginTop="16dp"        android:hint="input username"        android:inputType="textEmailAddress" />    <EditText        android:id="@+id/id_txt_password"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="16dp"        android:layout_marginLeft="4dp"        android:layout_marginRight="4dp"        android:layout_marginTop="4dp"        android:fontFamily="sans-serif"        android:hint="input password"        android:inputType="textPassword" /></LinearLayout>

下次讲一些Fragment的高级应用和一些封装的公共类的使用
再见

0 0
原创粉丝点击