Android : Accessing container activity object from fragment using putExtra?

前端 未结 2 1583
情歌与酒
情歌与酒 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:04

    There are multiple ways of achieving this.

    1. Make a static variable to hold your data and access that data from inside the fragments - this is the most fast but it creates bad design patterns if used improperly.
    2. A way of Fragment-to-Fragment communication possible through the parent Activity is posted here: http://developer.android.com/training/basics/fragments/communicating.html You can use the sample code to just do a Activity - Fragment data send.
    3. The top voted answer here: Accessing instance of the parent activity? mentions a way to avoid using static data (1.) and contains source code examples using ActivityGroup

    "If you need access to some values in your First activity without making a static reference to it, you could consider putting your activities in an ActivityGroup."

    What you choose is your preference, these are just a few options!


    Edit: I'm not sure if number 3 will work with fragments since I haven't tested a method similar to it, the example is Activity - Activity communication.

提交回复
热议问题