andriod fragment调用Activity函数方法
(1)新增一个activity
1 package com.xxxx.activity; 4 5 public interface FragmentCallBack { 7 8 public MainActivity getMainActivity(); 9 10 }
(2)在MainActivity 的Java文件中增加 getMainActivit公用方法
1 @Override 2 public MainActivity getMainActivity() { 3 return this; 4 }
(3)在fragment调用访问MainActivity 的所有公用方法;
1 //声明:注意要引用 2 private FragmentCallBack mFragmentCallBack; 3 4 5 //创建: 6 @Override 7 public void onAttach(Activity activity) { 8 super.onAttach(activity); 9 try { 10 mFragmentCallBack = (FragmentCallBack) activity; 11 } catch (ClassCastException e) { 12 throw new ClassCastException(activity.toString() 13 + " must implement OnHeadlineSelectedListener"); 14 } 15 } 16 17 18 //调用: 19 mFragmentCallBack.getMainActivity().showmsg();