为什么在创建片段时未保存包?

时间:2014-06-17 19:09:44

标签: android android-fragments

我在创建片段时尝试在包中传递一些数据,并且由于某种原因,当我在onCreate方法中读取它时,该包为空。

国家片段类

public class CountryFragment extends Fragment {
    public static final String EXTRA_COUNTRY_ID = "com.sitram.country_id";

    private Country mCountry;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        UUID countryID = (UUID)savedInstanceState.getSerializable(EXTRA_COUNTRY_ID);
        mCountry = CountryBank.get(getActivity()).getCountry(countryID);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.country_fragment, container, false);

        return v;
    }

    public static CountryFragment newInstance(UUID crimeID){
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_COUNTRY_ID, crimeID);

        CountryFragment fragment = new CountryFragment();
        fragment.setArguments(args);

        return fragment;
    }

}

CountryPagerActivity创建片段:

public class CountryPagerActivity extends FragmentActivity {
    private ViewPager mViewPager;
    private ArrayList<Country> mCountryList;

    @Override
    protected void onCreate(Bundle savedInstances) {
        super.onCreate(savedInstances);

        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.viewPager);
        setContentView(mViewPager);

        mCountryList = CountryBank.get(this).getCountryList();

        FragmentManager fm = getSupportFragmentManager();
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {

            @Override
            public int getCount() {
                return mCountryList.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                Country mCountry = mCountryList.get(arg0);
                return CountryFragment.newInstance(mCountry.getCountryId());
            }
        });

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                Country mCountry = mCountryList.get(arg0);
                if (mCountry.getContinentName() != null){
                    setTitle(mCountry.getCountryName());
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

        UUID countryID = (UUID)getIntent().getSerializableExtra(CountryFragment.EXTRA_COUNTRY_ID);
        for (int i=0; i<mCountryList.size(); i++) {
            if (mCountryList.get(i).getCountryId().equals(countryID)) {
                mViewPager.setCurrentItem(i);
                break;
            }
        }
    }

}

2 个答案:

答案 0 :(得分:2)

你是从savedInstanceState获得的,它只用于因方向改变,按下主页按钮等而再次创建的片段。

它应该来自片段的

<强>样品:

更改此内容:

UUID countryID = (UUID)savedInstanceState.getSerializable(EXTRA_COUNTRY_ID);

<强>

Bundle bundle = this.getArguments();
UUID countryID  = (UUID)bundle.getSerializable(EXTRA_COUNTRY_ID);

答案 1 :(得分:0)

您尝试访问的Bundle(onCreate()onCreateView()onActivityCreated())是在配置更改发生时调用onSaveInstanceState()时填充的。

您需要改为使用getArguments().getSerializable(EXTRA_COUNTRY_ID)