gpt4 book ai didi

android - onCreate() 在使用 ViewPager 更改 Fragment 的配置时调用了两次

转载 作者:行者123 更新时间:2023-11-29 01:08:56 25 4
gpt4 key购买 nike

一般背景:
我有一个带有 MainFragment 的 MainActivity。 MainFragment 有一个 ViewPager,我使用 FragmentStatePagerAdapter 根据 SqliteDatabase 的内容显示 DetailFragments。该数据库的内容在 LocationActivity 中是可见和可管理的。当我单击 LocationActivity 中的项目时,我使用 intent.putextra 附加项目的位置,然后在 MainFragment 的 ViewPager 中显示正确的位置。当我在 LocationActivity 中添加项目时,它会将项目的位置保存在 SharedPreferences 中,并且我会在 MainFragment 中获得正确的位置以显示我刚刚添加的项目的 fragment 。

问题:
它可以工作,但是当发生配置更改时,我会丢失 ViewPager 选择的实际页面。所以我用 onSaveInstanceState() 保存位置,如果它不为空,我在 onCreateView() 和 onViewCreated() 中检索它,我根据条件通过 if 语句获取 ViewPager.setCurrentItem() 所需的位置。但是随后第二次调用 onCreate() 时,onSaveInstanceState() 为空,我丢失了在配置更改之前选择的页面。你知道为什么它会被调用两次吗?我能做些什么来防止这种情况发生?感谢您的帮助

日志

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

一个最小的、完整的、可验证的例子

主要 fragment .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);
}
}

主要 Activity

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();
}
}

主要细节 fragment

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);
}
}

主页面适配器

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。您应该将 Activity 更新为如下所示:

@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 将经历它自己的初始化过程。这导致似乎多次调用 MainFragmentonCreate

关于android - onCreate() 在使用 ViewPager 更改 Fragment 的配置时调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44737400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com