TabStacker icon indicating copy to clipboard operation
TabStacker copied to clipboard

On changing tab from 1 to another. TabStackInterface.onTabFragmentPresented() returns getActivity()==null;

Open sandeeprana011 opened this issue 8 years ago • 3 comments

On changing tab from 1 to another. TabStackInterface.onTabFragmentPresented() returns getActivity()==null;

sandeeprana011 avatar Feb 24 '17 13:02 sandeeprana011

Hello Sandeep and thank you for your feedback.

You are right, getActivity always returns null in this callback :/

I'm not sure I can fix this, as this is how the lib and android work together:

  • fragments are restored from TabStacker, and callbacks are called on ALL fragments of the new tab
  • android makes asynchronous attachement later (then getActivity returns a valid activity) so it is too late, + the attachement is not done on all fragments, but only those supposed to be visible.

I will continue to investigate but for the moment I have no fix or workaround.

I can just suggest to move your code in onAttach() so that you are sure that the Activity is valid at this moment, but it won't be called on every fragment.

@Override
public void onAttach(Context context) {
    Activity activity = getActivity();
    // do something with the Activity
    super.onAttach(context);
}

Arnaud.

smart-fun avatar Feb 24 '17 22:02 smart-fun

Thank you @smart-fun for your quick response. Let this issue open for now so that subscribers can get the information

sandeeprana011 avatar Feb 27 '17 07:02 sandeeprana011

Did you manage to use TabStacker even if getActivity() returns null when the callbacks are called?

smart-fun avatar Mar 13 '17 15:03 smart-fun