8

I have a strange problem in which I encountered:The getActivity() method always returns null inside fragment. I calling it after the onAttach() and onCreateView() are finishing their run.

This fragment lives inside a FragmentActivity() that contains stack of fragments, and the way I adding fragment to it is:

(This code is being called from the onCreate() of the Fragment Activity())

SmartFragment fragment;
fragment = (SmartFragment) Fragment.instantiate(this,
fragmentClassName, params);     
mStackOfFragments.add(fragment);
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.add(R.id.tabcontent, fragment);
trans.addToBackStack(null);
trans.commitAllowingStateLoss();

I hope it is clear enough

Edit 1:

The call to getActivity():

protected OnDoneListener nDoneListener = new OnDoneListener() {

    @Override
    public void OnDone(final int counter, final String name) {


        if (getActivity() != null)
            ((TabActivity) getActivity()).RunOnUiThread(new Runnable() {

... ...

This callback is being called from a different class.

Edit 2:

class MemoryManager()
{

    private OnDoneListener nDoneListener;

    public void setOnDoneListener(OnDoneListener onDoneListener)
    {
    this.onDoneListner = onDoneListener;
    }

    public void updateUiOnRequestFinish()
    {
      onDoneListener.onDone();
    }

}

The MemoryManaget itself calls to updateUiOnRequestFinish() from a different callback

Edit 3:

The FragmentManager logs are:

04-08 18:44:05.950: V/FragmentManager(16280): Commit: BackStackEntry{41f9bd60}
`04-08 18:44:05.950: D/FragmentManager(16280):   mName=null mIndex=-1 mCommitted=false
04-08 18:44:05.950: D/FragmentManager(16280):   Operations:
04-08 18:44:05.950: D/FragmentManager(16280):     Op #0: ADD FragmentMyProfile{41f9bc20 id=0x7f070126}
04-08 18:44:05.950: V/FragmentManager(16280): Setting back stack index 0 to BackStackEntry{41f9bd60}
04-08 18:44:05.950: V/FragmentManager(16280): Run: BackStackEntry{41f9bd60 #0}
04-08 18:44:05.950: V/FragmentManager(16280): Bump nesting in BackStackEntry{41f9bd60 #0} by 1
04-08 18:44:05.950: V/FragmentManager(16280): Bump nesting of FragmentMyProfile{41f9bc20 id=0x7f070126} to 1
04-08 18:44:05.950: V/FragmentManager(16280): add: FragmentMyProfile{41f9bc20 id=0x7f070126}
04-08 18:44:05.950: V/FragmentManager(16280): Allocated fragment index FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
04-08 18:44:05.950: V/FragmentManager(16280): moveto CREATED: FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
04-08 18:44:05.950: V/FragmentManager(16280): moveto ACTIVITY_CREATED: FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
04-08 18:44:05.990: V/FragmentManager(16280): moveto STARTED: FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
04-08 18:44:06.030: V/FragmentManager(16280): moveto RESUMED: FragmentMyProfile{41f9bc20 #0 id=0x7f070126}
`

And it looks fine to me. So I'm guessing that this bug relates to the way I call the getActivity() from the callback.

Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
Nativ
  • 3,092
  • 6
  • 38
  • 69
  • 1
    it only strange if you are not familiar with Fragment lifecycle ... http://developer.android.com/guide/components/fragments.html#CoordinatingWithActivity ... also check few lines before this paragraph ... are you adding Fragment to Activity in Activity constuctor? – Selvin Apr 08 '13 at 12:41
  • Can you put the code with the getActivity() line ? – Arnaldo Ignacio Gaspar Véjar Apr 08 '13 at 12:41
  • Selvin: From your comment it implicates that this is a trivial issue, well I hope you are right, but you probably not. I know this link and its content and still having this problem – Nativ Apr 08 '13 at 12:51
  • Did you actually read over the page Selvin linked? _"Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. ..."_ I'm not sure what you mean by "after" onAttach and onCreateView, but a fragment is not attached _during_ those methods. Try calling getActivity from onActivityCreated or thereafter. Also, yes, please post where you're actually calling getActivity from. – Charlie Collins Apr 08 '13 at 14:12
  • Ok please see edit 2, the fragment should be attached to activity after the onAttach() onCreateView() and onResume() was called(the fragment is "active" according to the schema in the link). – Nativ Apr 08 '13 at 14:22
  • 1
    In cases like this, FragmentManager.enableDebugLogging(true) is a saviour. Look for anything that might be moving your fragment to STOPPED and onwards. Your fragment most certainly is not attached, or getActivity() would be giving you a reference. – Delyan Apr 08 '13 at 14:31

1 Answers1

7

This problem can be for used getActivity() of "android.app.Fragment" or "android.support.v4.app.Fragment"

if your are using "android.support.v4.app.Fragment" you need to review if you aren't using getActivity from "android.app.Fragment" or vice versa.

  • this solved my problem. i use getSupportActivity() instead of getActivity() as I am using Holo Everywhere. Still don't quite know why getActivity() was working when the fragment first loaded, but not when i would navigate away and then back... – speedynomads Jun 04 '13 at 17:41