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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With