Android : Accessing container activity object from fragment using putExtra?

前端 未结 2 1581
情歌与酒
情歌与酒 2021-02-07 13:06

I am building a tab interface using Action bar and fragment. I would need assistance in sending data from container activity to the fragment.

To elaborate, I have job ob

2条回答
  •  一向
    一向 (楼主)
    2021-02-07 14:05

    Make the method in your activity, e.g getJob that will return the Job object and its information

    MyActivity extends Activity{
    Job mJob;
    
    public Job getJob(){
       return this.mJob;
     }
    }
    

    then in your Fragment you do this:

    MyFragment extends Fragment{
    
    @Override
    public void onActivityCreated(){
      super.onActivityCreated();
      ((MyActivity)this.getActivity()).getJob();
     }
    }
    

    use getActivity and the method getJob(); to get the object

提交回复
热议问题