2

I have a FragmentActivity that has the values I needed to be used in a Fragment.

This is the way I do it, but I have no idea on how to access the getter in the FragmentActivity.

ItemDetailActivity

public class ItemDetailActivity extends FragmentActivity implements ActionBar.TabListener {

    public String getItem_id() {
        return item_id;
    }

    public String getUser_id() {
        return user_id;
    }
    ...
}

ItemPhotosFragment

public class ItemPhotosFragment extends Fragment {
    public ItemPhotosFragment() {
        user_id = getActivity().getUser_id();
    }
}

As you can see in the FragmentActivity, I'm implementing TabListener. So I'm want to pass the values to all tabs (fragments).

How do I do this? Is accessing from getActivity a best practice because I saw some of the solutions here suggesting it.

emen
  • 6,050
  • 11
  • 57
  • 94

3 Answers3

4

type cast into parent activity:

write this code in onActivityCreated inside your fragment not in constructor:

user_id =((ItemDetailActivity )getActivity()).getUser_id();

It's about fragment's life-cycle. onActivityCreated is called when fragment becomes part of the activity and this is the first callback from where getActivity() doesn't return null

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • 1
    `getActivity()` will return null if fragment is not attached – Raghunandan Jan 21 '14 at 08:16
  • 1
    @AimanB http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android. this could help – Raghunandan Jan 21 '14 at 08:19
  • you can write this code in any of the fragment functions but do ensure that those methods come after onAttach() in fragment life cycle. – SohailAziz Jan 21 '14 at 08:20
  • Raghu, that solution only works with specific fragments, am I right? Because I'm using tabs (multiple fragments) here. So I would want to access it all from one place. – emen Jan 21 '14 at 08:20
  • Do I need to put the methods in onAttach() method @SohailAziz? I did what Vipul said, and it also worked. What's the difference? Thanks anyway. – emen Jan 21 '14 at 08:29
  • 2
    it's about fragments life cycle. first onCreateView is called and onActivityCreated is called when becomes part of the activity and this is first callback from where getActivity() doesn't return null – vipul mittal Jan 21 '14 at 08:34
  • 1
    onAttach() is Called when the fragment has been associated with the activity (the Activity is passed in here). look at fragment [life cycle](http://developer.android.com/guide/components/fragments.html#Adding) – SohailAziz Jan 21 '14 at 08:34
1

There are two ways of doing this:

  1. While initializing the fragment in activity, you can do myFragment.setArguments(Bundle bundle) and you can put your params in this bundle.
  2. The other way is mentioned in above example.
Abhishek Shukla
  • 1,242
  • 8
  • 11
1

Above solution provided are good. i think the same can be done in a much cleaner way using delegation pattern. Here is the link for this. http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

ppuskar
  • 773
  • 6
  • 9