Вопрос

I have a simple Activity containing a ViewPager, which displays Fragments.

My Activity should display information about a football league, and each fragment displays information like livescroes/matchdays, tables, etc.

The Intent with which I start the Activity, contains the league id.

And each Fragment needs this league id to load the correct data.

So my FragmentPagerAdapter looks like this

public class LeaguePagerAdapter extends FragmentPagerAdapter {

    private String leagueId;

    public LeaguePagerAdapter(FragmentManager fm, String leagueId) {
        super(fm);
        this.leagueId = leagueId;
    }

    @Override
    public Fragment getItem(int pos) {
        if (pos == 0){
            return TableFragment.newInstance(leagueId);
        } else {         
            return MatchdayFragment.newInstance(leagueId);
        }
    }
}

The TableFragment looks like this ( the matchday fragment looks similar):

public class TableFragment extends PullToRefreshListViewAdFragment {

    private String leagueId;

    public static TableFragment newInstance(String leagueId) {
        TableFragment t = new TableFragment();
        t.leagueId = leagueId;
        return t;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
              // Setup UI and load data
        }
}

Sometimes the leagueId is null. I see the exceptions in the crash logs (crittercism). But Im asking my self why. It seems to me, that the problem is when the activity has been destroyed in the background and reconstructed if (for instance) the user uses the multitasking button to switch to my app.

So as far as I know, the original Intent will be stored internally by Android itself if the Activity has been destoryed. Therefore I have not implemented any onSaveInstanceState() in my activity nor in the fragment. In my activity I read the Intent Extra to retrieve the leagueId. This works fine, also on restoring the activity. I have assumed that by recreating the activity, a new LeaguePagerAdapter will be created and all fragments will also be new created.

Is that correct? Or does the "old" fragment instance will be restored and hence the leagueId is null (because the fragment has not stored the leagueId in Fragments onSaveInstanceState method?).

Is there a way to test such lifecycle things

Это было полезно?

Решение

The reason it is null is because the system restores the Fragment with the default constructor. Here's what the documents say:

Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().

edit: also, take a look at this: Fragment's onSaveInstanceState() is never called

edit: To further add on, you are creating your Fragment with your newInstance(String) method. If your Fragment is killed by Android, it uses the default constructor and so your leagueId variable won't be set. Try using setArguments/getArguments to pass the value into your Fragment instead.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top