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

问题描述

我有一个活动(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.

推荐答案

Define an Interface then implement it in the activity

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

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

07-29 17:24
查看更多