Chris
Chris

Reputation: 113

Android - ListView in TabLayout in Fragment gets null on getActivity

While trying to reopen the application after onDestroy() is called, I can't seem to update my ListView within my two Fragments ( Anytime, and Upcoming ) as they're nested within a TabLayout within my MainScreen's ViewPager.

When I return to my application, I need to update the ListViews with data I'm getting from my server, and my networking logic is handled within my MainScreen class so I can tell it's children what to display.

However, when I call FeedPagerAdapter's update method, my AnytimeFragment's getActivity() call often returns null. It doesn't return null if I call it during onCreateView(...), but awhile after ( loading server data ), it does return null. Although, if I cause onPause and onResume to be called after this, it'll display correctly.

I feel like I'm doing something incorrectly with how I'm handling my Fragments within my TabLayout, or getting them back after onDestroy().

This is the flow I'm using when trying to update my AnytimeFragment after I have acquired the data from my server.

MainScreen -> Handle to Feed Fragment -> FeedPagerAdapter's update -> Anytime's update.

Thanks for your help.

Main Screen Pager with three Fragments ( Feed, Create and Home ).

private class MainPagerAdapter extends FragmentStatePagerAdapter {

    public MainPagerAdapter ( FragmentManager fm ) {
        super( fm );
    }

    @Override
    public Fragment getItem( int position ) {
        switch ( position ) {
            default:
            case FEED:
            // Child with TabLayout and two Fragments I'm trying to update.
                return new Feed(); 
            case CREATE:
                return new Create();
            case HOME:
                return new Home();
        }
    }

    @Override
    public int getCount() {
        return 3;
    }
}

Feed TabLayout with two Fragments ( Anytime, and Upcoming ).

public class FeedPagerAdapter extends FragmentPagerAdapter {

    public FeedPagerAdapter ( FragmentManager fm, Context context ) {
        super( fm );
        this.context = context;
    }

    @Override
    public Fragment getItem( int position ) {
        switch ( position ) {
            default:
            case ANYTIME:
                return new AnytimeFragment();
            case UPCOMING:
                return new UpcomingFragment();
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    public void update() {
        ( ( AnytimeFragment) getItem( ANYTIME) ).update();
    }
}

Within my Anytime Fragment.

public void update() {
    // This often returns null.
    if( getActivity() == null )
        return;

    // Update the ListView.

}

Upvotes: 0

Views: 319

Answers (1)

Chris
Chris

Reputation: 113

I found out how to fix my problem.

First, I was recreating my FeedPagerAdapter onResume within my Feed class. I noticed it would call Anytime.update() more times than I wanted.

Second, I changed my listView within the Anytime Fragment to be static, as it was sometimes null.

Upvotes: 1

Related Questions