首页 > 代码库 > Android_Fragment和Activity之间的通信
Android_Fragment和Activity之间的通信
Fragment 的生命周期是随着activity变化而变化的。
如果activity要给在运行的时候给fragment传人数据则需要fragment实现一个自定义的接口,并且实现接口里面的方法,在activity里面保存这个接口并且在需要传人数据的时候调用这个方法即可。
如果fragment运行的时候需要给activity传数据了?activity先实现这个接口,然后在fragment里面重写onAttach(Activity act)这个方法,并在这个方法里面讲activity强制转换为接口类,然后在fragment使用的时候调用这个接口类的方法。
请看下面的例子:
activity给fragment里面传人数据:
public class MainActivity extends Activity{ private TestFragment fragment;//fragment private UpdateFragment updateFragment;//fragment实现的接口 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); initView(); } private void initView() { FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragment = new TestFragment(); updateFragment = fragment; fragmentTransaction.add(R.id.main, fragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commitAllowingStateLoss(); } @Override protected void onResume() { //改变fragment里面textView的值 updateFragment.setData("王五"); super.onResume(); }}
接口类
public interface UpdateFragment { void setData(String str);}
Fragment类:
public class TestFragment extends Fragment implements UpdateFragment{ private View view; private TextView txt; public TestFragment() { super(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (null == view){ view = inflater.inflate(R.layout.activity_main, container, false); txt = (TextView)view.findViewById(R.id.tv); } return view; } @Override public void setData(String str) { if (null != txt) { txt.setText(str); } }}
Fragment给activity传入数据demo:
Activity类:
public class MainActivity extends Activity implements UpdateFragment{ private static final String TAG = MainActivity.class.getSimpleName(); private TestFragment fragment;//fragment @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); initView(); } private void initView() { FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragment = new TestFragment(); fragmentTransaction.add(R.id.main, fragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commitAllowingStateLoss(); } @Override protected void onResume() { super.onResume(); } @Override public void setData(String str) { if (null != str ){ Log.d(TAG, "setData:"+str); } }
接口类和上面的是一样的就不写了
Fragment类:
public class TestFragment extends Fragment{ private View view; private TextView txt; private Button btn; private UpdateFragment updateFragment; public TestFragment() { super(); } @Override public void onAttach(Activity activity) { try { updateFragment = (UpdateFragment)activity; } catch (Exception e) { e.printStackTrace(); } super.onAttach(activity); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (null == view){ view = inflater.inflate(R.layout.activity_main, container, false); txt = (TextView)view.findViewById(R.id.tv); btn = (Button)view.findViewById(R.id.btn); btn.setOnClickListener(clickListener); } return view; } private OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View v) { if (null != txt) { updateFragment.setData(txt.getText().toString()); } } };}
Android_Fragment和Activity之间的通信
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。