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
- Android Fragment的使用 七 Argument和startActivityForResult传递数据
- 通过fragment argument从Activity传递数据到Fragment
- Android中Fragment之间的跳转和数据的传递
- Android fragment和activity之间的数据传递交互
- Android Activity、Fragment之间的数据传递和返回
- Android Fragment和Activity数据或对象的传递总结
- 关于Activity和Fragment同时使用startActivityForResult的问题
- Android点滴-Activity和Fragment 传递数据
- Android activity Bundle和fragment数据传递
- Android fragment数据传递
- Android Fragment数据传递
- 使用EventBus代替startActivityForResult向上传递数据
- Android startActivityForResult 和 setResult的使用
- Android Activity/Fragment间的数据传递
- Activity之间startActivityForResult传递和接受数据
- 在Fragment中使用startActivityForResult和onActivityResult
- fragment和activity之间的数据传递
- Fragment和Activity之间的数据传递
- 用new来分配在栈上的空间
- 求职准备418
- java学习之:JDBC与mysql
- CSS样式中的通用选择器和伪类选择器
- 商品详情页中,当鼠标放到小图上时,改变大图的图片为当前小图的图片
- Android Fragment的使用 七 Argument和startActivityForResult传递数据
- POJ 3481 Double Queue(Splay)
- C++实现二叉查找树
- [BZOJ2539][Ctsc2000]丘比特的烦恼(KM+计算几何)
- java学习之:复写equals方法
- 其他类型转化为json
- 基础排序算法-快速排序
- PAT 1002. A+B for Polynomials (25)
- leetcode404: Sum of Left Leaves