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()); } } }; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。