面试的时候是不是经常被面试官问一句:Fragment和Activity之间怎样通讯?然后就懵了?其实很简单的,首先Fragemtn就是一个普通的java类,它并没有继承其他的父类。而且,Fragment总是由你自己new出来,因此要通讯最高效的方式当然就是持有引用,调用方法了。下面看看深圳的技术辅导老师(同时也叫就业指导老师)在问答网上关于这个问题的回答。
学生问题:老师我遇到一个工作上面的问题,需要从fragment传递数据到activity 老师回答: 第一种数据方式fragment和activity,方式是使用getacitivity方法传递数据: 代码如下: 例如:从acitivity中传递fragment,fragment里面如下: - ((MainActivity)getActivity()).setmTitle("aaaaaaaaaaaa");
复制代码 发送数据的acitivity里面这需要写如下代码:- public void setmTitle(String title) {
- this.mTitle = title;
- }
复制代码 第二种,如果是同一个Activity中要加载很多Fragment的话、这种方法无疑要简单很多
第二种传递方式,采用fragment原生的apisetargusments方法。
acitivity里面代码:
- //事务
- ft = fm.beginTransaction();
- StrogeFragment fragment = new StrogeFragment();
- Bundle bundle = new Bundle();
- bundle.putString("key", "这是方法二");
- fragment.setArguments(bundle);
-
-
- ft.replace(R.id.container, fragment);
- ft.commit();
复制代码 fragment里面的接收代码:
- string = getArguments().getString("key");
- Log.e("StrogeFragment",string);
复制代码
第三种传递方式可以采用接口:
接口的代码:
- package com.example.fragmentback;
-
- import android.os.Bundle;
-
- public interface FragmentCallBack {
- public void callbackFun1(Bundle arg);
-
- public void callbackFun2(Bundle arg);
- }
复制代码 第一个fragment类:
- package com.example.fragmentback;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.Button;
-
-
-
- public class Fragmentone extends Fragment {
-
- FragmentCallBack fragmentCallBack = null;
- private Button btn;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment1, container,
- false);
- btn = (Button)rootView.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- fragmentCallBack.callbackFun1(null);
- }
- });
- return rootView;
- }
-
- @Override
- public void onAttach(Activity activity) {
- // TODO Auto-generated method stub
- super.onAttach(activity);
- fragmentCallBack = (MainActivity)activity;
- }
- }
复制代码 第二个fragment类:
- package com.example.fragmentback;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.Toast;
-
-
- public class Fragmenttwo extends Fragment {
-
- FragmentCallBack fragmentCallBack = null;
- Button btn1;
- Button btn2;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment2, container,
- false);
- Bundle data = getArguments();//获得从activity中传递过来的值
- Toast.makeText(getActivity(), data.getString("TEXT"), Toast.LENGTH_SHORT).show();
- btn1 = (Button)rootView.findViewById(R.id.btn1);
- btn1.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // 直接调用Activity中的方法
- ((MainActivity) getActivity()).changeButtonColor();
- }
- });
- btn2 = (Button)rootView.findViewById(R.id.btn2);
- btn2.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // 回调的方式
- fragmentCallBack.callbackFun2(null);
- }
- });
- return rootView;
- }
-
- @Override
- public void onAttach(Activity activity) {
- // TODO Auto-generated method stub
- super.onAttach(activity);
- fragmentCallBack = (MainActivity)activity;
- }
- }
复制代码 主要activity类:
- package com.example.fragmentback;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends FragmentActivity implements FragmentCallBack{
-
- private Button btn;
-
- private Fragmentone fragment1;
- private Fragmenttwo fragment2;
- private FragmentManager fragmentManager;
- private Fragment currentFragment;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- fragmentManager = getSupportFragmentManager();
-
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
- fragment1 = new Fragmentone();
- Bundle data = new Bundle();
- data.putString("TEXT", "这是Activiy通过Bundle传递过来的值");
- fragment1.setArguments(data);//通过Bundle向Activity中传递值
- fragmentTransaction.add(R.id.rl_container,fragment1);//将fragment1设置到布局上
- fragmentTransaction.addToBackStack(null);
- fragmentTransaction.commitAllowingStateLoss();
- currentFragment = fragment1;
- //初始化button控件
- btn = (Button)findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if(currentFragment instanceof Fragmentone){
- switchFragment();
- }else{//当前是fragment2,因此,只需要将fragment2出栈即可变成fragment1
- fragmentManager.popBackStack();
- currentFragment = fragment1;
- }
- }
- });
- }
- /**
- * 切换Fragment
- */
- private void switchFragment(){
- if(null == fragment2){//可以避免切换的时候重复创建
- fragment2 = new Fragmenttwo();
- }
- Bundle data = new Bundle();
- data.putString("TEXT", "传递给fragment2");
- fragment2.setArguments(data);
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
-
- fragmentTransaction.replace(R.id.rl_container,fragment2);
- fragmentTransaction.addToBackStack(null);
- fragmentTransaction.commitAllowingStateLoss();
- currentFragment = fragment2;
- }
-
- public void changeButtonColor(){
- btn.setBackgroundColor(Color.RED);
- }
-
- @Override
- public void callbackFun1(Bundle arg) {
- switchFragment();//通过回调方式切换
- }
-
- @Override
- public void callbackFun2(Bundle arg) {
- changeButtonColor();//通过回调方式调用Activity中的方法
- }
-
- }
复制代码
这是采用java接口原理来实现。
深圳校区除了全国独有问答网,就业老师面试服务,还有更多神秘惊喜等着你,咨询热线:0755-66689855
|