fragment与Activity之间的数据传递
2016-01-16 00:22
246 查看
Fragment与Activity通信(Fragment依靠在Activity中):
1.Fragment可调用getActivity()方法获取它所在的Activity对象
2.Activity可调用FragmentManager的findFragmentById()
--或findFragmentByTag()方法获取Fragment对象
3.Activity——>Fragment:在Activity中创建Bundle数据包,
--并调用Fragment的setArguments(Bundle bundle)方法发送bundle
--数据,在Fragment中调用getArguments(Bundle bundle)
--(动态传值:Fragment添加到Activity之前传值)
4.Fragment——>Activity:需要在Fragment中定义一个内部回调接口,
--再让包含该Fragment的Activity实现该回调接口。
--这样Fragment可调用该回调方法将数据传递给Activity
--注:
--Fragment中接口对象调用接口的方法
--(这里Activity实现了接口可以强转Activity对象得到接口对象)
--实际就是调用被Activity实现了的(接口)方法—>称为回调;
5、静态fragment的传值:
--FragmentManager fg= getFragmentManager();
--Fragment fg.findFragmentById(R.id.frag);
--MyFragment frag = (MyFragment)fg;// 强转
--frag.setXxx("fragment静态传值");
--然后再Fragment中调用get方法:获取值
这里我写了一个Activity一个Fragment它们共用一个布局,
实现了Activity—>Fragment和Fragment——》Activity的通信:
布局:
1.Fragment可调用getActivity()方法获取它所在的Activity对象
2.Activity可调用FragmentManager的findFragmentById()
--或findFragmentByTag()方法获取Fragment对象
3.Activity——>Fragment:在Activity中创建Bundle数据包,
--并调用Fragment的setArguments(Bundle bundle)方法发送bundle
--数据,在Fragment中调用getArguments(Bundle bundle)
--(动态传值:Fragment添加到Activity之前传值)
4.Fragment——>Activity:需要在Fragment中定义一个内部回调接口,
--再让包含该Fragment的Activity实现该回调接口。
--这样Fragment可调用该回调方法将数据传递给Activity
--注:
--Fragment中接口对象调用接口的方法
--(这里Activity实现了接口可以强转Activity对象得到接口对象)
--实际就是调用被Activity实现了的(接口)方法—>称为回调;
5、静态fragment的传值:
--FragmentManager fg= getFragmentManager();
--Fragment fg.findFragmentById(R.id.frag);
--MyFragment frag = (MyFragment)fg;// 强转
--frag.setXxx("fragment静态传值");
--然后再Fragment中调用get方法:获取值
这里我写了一个Activity一个Fragment它们共用一个布局,
实现了Activity—>Fragment和Fragment——》Activity的通信:
package zxx.activity_fragment; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.widget.Toast; import zxx.activity_fragment.MyFragment.Intergo; public class MainActivity extends FragmentActivity implements Intergo{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bundle mbundle=new Bundle(); mbundle.putString("KEY", "这是activity的值"); MyFragment fragment=new MyFragment(); fragment.setArguments(mbundle); getSupportFragmentManager().beginTransaction(). add(R.id.activity_ids, fragment).commit(); } @Override public void go(String s) { Toast.makeText(this, ""+s, Toast.LENGTH_LONG).show(); Log.i("*****", ""+s); } }
package zxx.activity_fragment; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MyFragment extends Fragment { String s; public Intergo mintergo; interface Intergo{ public void go(String s); } public MyFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.activity_main, null); TextView text= (TextView) v.findViewById(R.id.activity_text); Bundle b=getArguments(); s=b.getString("KEY"); text.setText(s); mintergo.go("fragment返回了"+s); return v; } @Override public void onAttach(Context context) { // TODO Auto-generated method stub super.onAttach(context); mintergo=(Intergo)context; } }
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_ids" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="zxx.activity_fragment.MainActivity" android:orientation="vertical"> <TextView android:id="@+id/activity_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一个Activity" /> </LinearLayout>
相关文章推荐
- Java图形界面设计——substance皮肤
- Android Design Support Library(三)用CoordinatorLayout实现Toolbar隐藏和折叠
- Android Design Support Library(三)用CoordinatorLayout实现Toolbar隐藏和折叠
- Spark编程指南笔记
- 毕业至今,微博写给自己的感悟的话
- java随机数:彩票抽奖 + 验证码
- Struts,Hibernate,Spring经典面试题
- Android error:C:\Program Files\Android\android-sdk\temp\sysimg_x86-10_r02.zip (拒绝访问。)
- 矩阵索引的使用——裁剪,颠倒
- Spring面试问答Top 25
- Wireshark与设备解析字节不一致问题
- js study
- 我的第一个简单的爬虫程序
- CSS中常用的清除浮动(闭合浮动)的方法
- Java面试题集(181-185)
- Cannot change version of project facet Dynamic Web Module to 2.5.
- 让TextView里面的文字逐个显示的动画效果实现(1)
- POJ 3159 Candies
- JavaScript基本用法
- Java面试题集(151-180)