0

I have 3 fragments that are contained within the main activity via a ViewPager. What I'm trying to do is allow myself to call methods on objects of those fragment classes from different lifecycle callbacks in the main activity, namely onRestart(), so that I can refresh their content via a backend call. To be more specific, one of the fragments contains a "live feed" of images that people are posting, and the user gets to this posting activity via one of the fragments.

Right now, the problem I'm trying to solve is that when a user goes to this posting activity and puts up a picture, the live feed in the fragment they come back to isn't getting refreshed. To do this, I would need to call my backend and UI update methods in that fragment from the main activity's onRestart().

But I'm running into these null pointer exceptions -- here's what the code looks like in the fragment:

  public void refreshUI(){

    activity = getActivity();
    appContext = ThisApp.appContext();
    view = getView();

    peopleHeader = (TextView) view.findViewById(R.id.people_header);
    peopleLinearLayout = (LinearLayout) view.findViewById(R.id.local_people_linearlayout);

    ..... (various other instantiations)... }

The NPEs are coming either from getActivity() or some of the UI instantiations. I call this code from onActivityCreated() and it works as expected. However, if I try to call this code again on my fragment object from onRestart(), I crash.

What's happening? Can't figure out why this would go bad on me.

Thanks so much.

Elltz
  • 10,730
  • 4
  • 31
  • 59
nectaris
  • 85
  • 7
  • You're calling it on the fragment in the activities onRestart()? – AdamMc331 Apr 24 '15 at 20:39
  • Are you using a FragmentTransaction somewhere in the activity to create the fragment the first time? Have you tried replacing your onRestart() code to replace the fragment *again*? – AdamMc331 Apr 24 '15 at 20:40
  • @McAdam331 -- Yep. I have an object of the FeedFragment, and so the calls from onRestart() are feedFragment.refreshUI() and feedFragment.getNewContent(). I only added the refreshUI() as an attempt to cure some of my problems with the apparently null items needed in getNewContent(). I'm not using FragmentTransactions anywhere, either. When I create the fragment for the first time, it all goes from onActivityCreated() and onViewCreated(). The way I set stuff up with the ViewPager doesn't use FragmentTransaction anywhere. – nectaris Apr 24 '15 at 20:47

1 Answers1

2

When your Activity is destroyed and recreated, your Fragments are also destroyed and recreated with it. You can have a look at the Fragment/Activity lifecycle explanation here.

What this means is that the reference you keep disappears, the Fragment is there but it is another object. You need to get a reference to this new object.

You can check this answer on how to do that. It explains getting references to Fragments created by a ViewPager adapter.

Community
  • 1
  • 1
Oguz Babaoglu
  • 299
  • 2
  • 7