Fragment回调传值到Activity问题
看作业,主界面点击进入详细页面需要两个fragment跳转。感觉需要fragment传输信息给Activity,在Activity判断切换Fragment。这部分在基础篇讲过了,使用无问题。但是我发现Android stuido自动生成的带回调接口的Fragment和基础篇的方法有些不同。看上去是重载了onAttach进行接口方法的设置
对于这个不清楚在Activity里怎么使用,模仿基础篇的方式尝试这么写mainFragment.onAttach(this),提示
onAttach is deprecated
正在回答
请截图说明,不太清楚你说的是哪个方法。祝:学习愉快
//自动生成的Fragment代码 public class MainFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public MainFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment MainFragment. */ // TODO: Rename and change types and number of parameters public static MainFragment newInstance(String param1, String param2) { MainFragment fragment = new MainFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_main, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(String msg) { if (mListener != null) { mListener.onFragmentInteraction(msg); } } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(String msg); } }
MainActivity实现了MainFragment.OnFragmentInteractionListener接口:
重载了public void onFragmentInteraction(String msg) {}
然后onCreate中载入Fragment代码
mainFragment=MainFragment.newInstance("test1","test2"); this.getSupportFragmentManager() .beginTransaction() .add(R.id.container_content,mainFragment) .commit();
如果这么写,软件会报错,查看是onAttach那里执行到抛出异常的分支。
尝试这么加上这么一句会提示onAttach is deprecated.
基础篇里是另开了一个方法来设置接口方法,调用那个方法就行了,没有写在重载的onAttach中。不知道这种情况怎么处理。
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星