一般情况:
我有一个带有MainFragment的MainActivity。 MainFragment有一个ViewPager,我使用FragmentStatePagerAdapter来基于SqliteDatabase的内容显示DetailFragments。该数据库的内容在LocationActivity中可见且可管理。当我在LocationActivity中单击某个项目时,我使用intent.putextra附加该项目的位置,然后在MainFragment中的ViewPager中显示正确的位置。当我在LocationActivity中添加项目时,它将项目的位置保存在SharedPreferences中,并且我在MainFragment中获得正确的位置以显示刚刚添加的项目的片段。
问题:
它可以工作,但是当发生配置更改时,我会丢失从ViewPager中选择的实际页面。所以我用onSaveInstanceState()保存该位置,如果它不为null,则在onCreateView()和onViewCreated()中检索它,根据条件通过if语句来获取ViewPager.setCurrentItem()所需的位置。但是然后再次调用onCreate(),然后onSaveInstanceState()为null,并且丢失了配置更改之前选择的页面。你知道为什么叫两次吗?我该怎么做才能防止这种情况?谢谢你的帮助
日志记录
06-24 09:15:05.974 activityMain.MainFragment: onLoadFinished() onPageSelected() 2
06-24 09:15:08.276 activityMain.MainFragment: onSaveInstanceState() 2
06-24 09:15:08.320 activityMain.MainFragment: onCreate()
06-24 09:15:08.342 activityMain.MainFragment: onCreateView()
06-24 09:15:08.342 activityMain.MainFragment: onCreateView() savedInstanceState Position: 2
06-24 09:15:08.349 activityMain.MainFragment: onViewCreated() get position from mCurrentPositionState 2
06-24 09:15:08.349 activityMain.MainFragment: onActivityCreated()
06-24 09:15:08.394 activityMain.MainFragment: onCreate()
06-24 09:15:08.396 activityMain.MainFragment: onCreateView()
06-24 09:15:08.401 activityMain.MainFragment: onViewCreated()) get position from SharedPreferences 4
06-24 09:15:08.401 activityMain.MainFragment: onActivityCreated()
06-24 09:15:08.407 activityMain.MainFragment: onResume()
06-24 09:15:08.458 activityMain.MainFragment: restartCursorLoader
06-24 09:15:08.508 activityMain.MainFragment: onLoadFinished()
06-24 09:15:08.537 activityMain.MainFragment: onLoadFinished() setCurrentItem: 4
最小,完整且可验证的示例
MainFragment.java
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
private static final String TAG = MainFragment.class.getName();
private static final String PAGE_SELECTED = "state_instance";
private MainPagerAdapter mAdapter;
private ViewPager mViewPager;
private int mLocationPosition;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
mLocationPosition = savedInstanceState.getInt(PAGE_SELECTED);
Log.i(TAG, "onCreateView() savedInstanceState not null, position: " + mLocationPosition);
} else {
mLocationPosition = 0;
Log.i(TAG, "onCreateView() savedInstanceState null, position: " + mLocationPosition);
}
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
mAdapter = new MainPagerAdapter(getActivity().getSupportFragmentManager());
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewPager.setAdapter(mAdapter);
mViewPager.setCurrentItem(mLocationPosition, false);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mLocationPosition = position;
Log.i(TAG, "onActivityCreated() mLocationPosition value: " + mLocationPosition);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(PAGE_SELECTED, mLocationPosition);
Log.i(TAG, "onSaveInstanceState() mLocationPosition value: " + mLocationPosition);
super.onSaveInstanceState(outState);
}
}
主要活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new MainFragment());
fragmentTransaction.commit();
}
}
MainDetailFragment
public class MainDetailFragment extends Fragment {
private static final String TAG = MainDetailFragment.class.getName();
private TextView mTextViewLocation;
protected static final String ARGUMENT_PAGE = "location_page";
protected static final String ARGUMENT_NAME = "location_name";
private String mLocation;
public MainDetailFragment() {
}
protected static MainDetailFragment newInstance(int page, String locationName) {
MainDetailFragment mainDetailFragment = new MainDetailFragment();
Bundle arguments = new Bundle();
arguments.putInt(MainDetailFragment.ARGUMENT_PAGE, page);
arguments.putString(MainDetailFragment.ARGUMENT_NAME, locationName);
mainDetailFragment.setArguments(arguments);
return mainDetailFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARGUMENT_NAME)) {
mLocation = getArguments().getString(ARGUMENT_NAME);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main_detail, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTextViewLocation = (TextView) view.findViewById(R.id.tv_city_name);
mTextViewLocation.setText(mLocation);
}
}
MainPagerAdapter
public class MainPagerAdapter extends FragmentStatePagerAdapter {
private static final String TAG = MainPagerAdapter.class.getName();
private static int NUM_ITEMS = 3;
public MainPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// 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 MainDetailFragment.newInstance(0, "Paris");
case 1:
return MainDetailFragment.newInstance(1, "London");
case 2:
return MainDetailFragment.newInstance(2, "New York");
default:
return null;
}
}
}
最佳答案
您需要确保在配置更改后没有第二次添加MainFragment。您应该将“活动”更新为如下形式:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (savedInstanceState != null) {
return;
}
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new MainFragment());
fragmentTransaction.commit();
}
原因是
Activity
本身已经具有添加的MainFragment
记录,并且将在更改配置后自动将其还原。如果再次执行相同的事务,首先您将看到恢复的MainFragment
正在启动,然后将其替换为新Fragment
事务中的新事务,而新的,不同的MainFragment
将要运行通过自己的初始化过程。这导致对onCreate
似乎多次调用MainFragment
。