获取变量引用的片段活动 [英] Getting variable reference from fragment to activity

查看:108
本文介绍了获取变量引用的片段活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动(MainActivity)持有一个片段(碎裂)与的ArrayList<乌里> array_list =新的ArrayList<乌里>(); 。 如何让我的MainActivity获取array_list的参考?

I have an activity (MainActivity) that holds a Fragment (FragmentA) with an ArrayList<Uri> array_list = new ArrayList<Uri>();. How can my MainActivity get a reference of that array_list?

现在,如果你问我,你为什么我想要得到的片段认为是拥有该片段,我会说一个活动的变量的引用,我打算让项目在array_list当我点击我MainActivity操作按钮。

Now if you ask me why'd I want to get a reference of a variable of a fragment to an activity that holds that fragment, I'd say, I'm planning to get the items in array_list when I click the action button on my MainActivity.

我使用ViewPager。我没有片段的ID。

I'm using ViewPager. I don't have fragment id's.

推荐答案

<一个href="https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCMQFjAA&url=http://developer.android.com/training/basics/fragments/communicating.html&ei=jBf1UviZF_Pa0QXozYDIBg&usg=AFQjCNGQCzxA1VfoENYTJisTDtnWzl9hEA&sig2=zkLyAlGOlEf6JM_gMu8ViQ"相对=nofollow>这里是一个很好的政党成员:

Here is a good tuto :

定义一个接口,然后在活动执行它

Define an Interface then implement it in the activity

要允许一个片段最多传达到它的活动,你可以定义在片段类的接口,并在活动中实现它。该片段捕捉接口实现其onAttach()的生命周期方法的过程中,然后可以调用接口的方法,以便与活动进行沟通。

To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods in order to communicate with the Activity.

这篇关于获取变量引用的片段活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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