我在此活动中有一个自定义的viewPagerAdapter,当首次启动活动时(当主活动按意图启动时),分页器会正确显示片段,但是当我旋转设备时,配方是从saveedInstantState获取的,并且适配器已启动,但不显示片段,也不会调用getItem方法!

这是活动的代码:
包com.ameer.bake.activities;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.ViewGroup;

import com.ameer.bake.Constants;
import com.ameer.bake.fragments.IngredientsFragment;
import com.ameer.bake.fragments.StepDetailsFragment;
import com.ameer.bake.fragments.StepsFragment;
import com.ameer.bake.R;
import com.ameer.bake.models.Recipe;
import com.ameer.bake.models.Step;
import com.google.gson.Gson;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

public class DetailsActivity extends AppCompatActivity implements StepsFragment.StepCallback{

    private Recipe recipe;
    private IngredientsFragment ingredientsFragment;
    private StepsFragment stepsFragment;



   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    if (savedInstanceState != null){
        recipe = new Gson().fromJson(savedInstanceState.getString(Constants.RECIPE), Recipe.class);
        setupViewPager();
    } else if (getIntent().hasExtra(Constants.CURRENT_RECIPE_KEY) ) {
        Gson gson = new Gson();
        recipe = gson.fromJson(getIntent().getStringExtra(Constants.CURRENT_RECIPE_KEY), Recipe.class);
        setTitle(recipe.getName());
        setupViewPager();
    }

}

@Override
public void onStepClicked(Step step) {
    Intent intent = new Intent(DetailsActivity.this, StepActivity.class);
    intent.putExtra(Constants.STEP_KEY, new Gson().toJson(step));
    startActivity(intent);
}

private class RecipePagerAdapter extends FragmentPagerAdapter {
    private static final int NUM_ITEMS = 2;
    private final String[] titles = new String[]{
            getString(R.string.ingredients), getString(R.string.steps)};

    private RecipePagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        if (ingredientsFragment == null && stepsFragment == null) {
            ingredientsFragment = new IngredientsFragment();
            ingredientsFragment.setIngredients(recipe.getIngredients());
            stepsFragment = new StepsFragment();
            stepsFragment.setSteps(recipe.getSteps());
            stepsFragment.setCallback(DetailsActivity.this);
        }
    }

    // Returns total number of pages
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    // Returns the fragment to display for that page
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return ingredientsFragment;
            case 1:
                return stepsFragment;
            default:
                return null;
        }
    }

    // Returns the page title for the top indicator
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home){
        NavUtils.navigateUpFromSameTask(this);
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString(Constants.RECIPE, new Gson().toJson(recipe));
    super.onSaveInstanceState(savedInstanceState);
}

private void setupViewPager(){
    ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
    FragmentPagerAdapter pagerAdapter = new RecipePagerAdapter(getSupportFragmentManager());
    vpPager.setAdapter(pagerAdapter);

    SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpager_tab);
    viewPagerTab.setViewPager(vpPager);
}


}

最佳答案

切换到FragmentStatePagerAdapter而不是使用FragmentPagerAdapter。同时使用getChildFragmentManager()而不是getSupportFragmentManager()

09-28 14:15