FragmentViewPagerAdapter在屏幕旋转后不调用getItem

时间:2017-09-19 16:10:10

标签: java android android-fragments adapter fragmentpageradapter

我在此活动中有一个自定义viewPagerAdapter,当第一次启动活动时(当主要活动以意图启动它时),寻呼机正确显示碎片,但是当我旋转设备时,配方来自savedInstantState,启动适配器,但不显示片段,并且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);
}

}

2 个答案:

答案 0 :(得分:0)

切换到FragmentStatePagerAdapter而不是使用FragmentPagerAdapter。也可以使用getChildFragmentManager()而不是getSupportFragmentManager()

答案 1 :(得分:0)

解决方案正如Pedro Varela在评论中提到的那样: “希望这样做。添加setRetainInstance(true);在视图分页器的内部片段的onCreate中”控制是否在活动重新创建(例如从配置更改)中保留片段实例。这只能用于不在后栈中的片段。如果设置,则重新创建活动时片段生命周期将略有不同“” 感谢