安卓:使用putExtra片段访问容器活动的对象? [英] Android : Accessing container activity object from fragment using putExtra?

查看:121
本文介绍了安卓:使用putExtra片段访问容器活动的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立使用操作栏和片段的选项卡界面。我需要把从容器中的活动数据,以片段的援助。

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

要详细说明,我在容器activty作业对象。而且我已经创建基于在作业对象(如公司的详细信息,经验,信息等)的信息很少的设置页。我需要将作业对象传递给这些片段,这样它可以显示相应的信息。

To elaborate, I have job object in container activty. And I have created few tabs based on the information in job object (like company details, experience details etc). I need to pass the job object to these fragments so that it can display respective information.

我已经创建容器活动和标签片段。我需要就如何传递对象在它们之间的一个例子。我不能使用intent.putExtra。我可以从片段访问父容器中的对象?

I have created container activity and tab fragments. I would need an example on how to pass the object across them. I cannot use intent.putExtra. Can I access parent container's object from fragment?

任何帮助应是AP preciated。

Any help shall be appreciated.

感谢。

推荐答案

请该方法在您的活动,如: getJob 将返回工作对象及其信息

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();
 }
}

使用 getActivity 和方法 getJob(); 来获取对象

这篇关于安卓:使用putExtra片段访问容器活动的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆