Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

android - Getting variable reference from fragment to activity

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?

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a good tuto :

Define an Interface then implement it in the activity

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...