gpt4 book ai didi

android - 为什么会调用fragment的onCreateView、onCreate、onActivityCreated

转载 作者:IT老高 更新时间:2023-10-28 22:06:39 25 4
gpt4 key购买 nike

我有一个处理 fragment 和 ViewPager 的应用程序。我在 ViewPager 中有三个 fragment 。当您在它们之间切换时,它总是会导致其他两个 fragment 调用它们的 onCreateView 方法。如何只做一次,只有在创建 FragmentActivity 时才做???我已经阅读了一些问题并尝试了解决方案,但 fragment 仍然具有相同的行为。

ListFragment onCreate called twice
onCreate() and onCreateView() invokes a lot more than required (Fragments)

这里有一些代码,如果对你们有帮助的话,伙计们:

主 Activity :

public class StartingActivity extends FragmentActivity implements View.OnClickListener {
ViewPager viewPager;
CirclePageIndicator pageIndicator;

Button discount;
Button qrCode;
Button pay;
TabHost tabHost;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.starting_layout);
viewPager = (ViewPager) findViewById(R.id.pager);

if (savedInstanceState == null) {

Fragment firstPage = Fragment.instantiate(this, FindTovarFragment.class.getName());
Fragment secondPage = Fragment.instantiate(this, MainWindowActivity.class.getName());
Fragment thirdPage = Fragment.instantiate(this, MapActivity.class.getName());

if ((firstPage != null && !firstPage.isDetached())|| (secondPage != null && !secondPage.isDetached()) || (thirdPage != null && !thirdPage.isDetached())) {

List<Fragment> viewPagerFragments = new ArrayList<Fragment>();
viewPagerFragments.add(firstPage);
viewPagerFragments.add(secondPage);
viewPagerFragments.add(thirdPage);


PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager(), viewPagerFragments);

viewPager.setAdapter(pageAdapter);

pageIndicator = (CirclePageIndicator) findViewById(R.id.circle);
pageIndicator.setViewPager(viewPager);
pageIndicator.setCurrentItem(pageAdapter.getCount() - 2);
}
}

}

map Activity :

public class MapActivity extends Fragment implements OnMyLocationListener {

//Тэг для логов
private static final String TAG = "MapActivity";
List<Address> addressList;
private static final String STRING_LOCATION = "";

ArrayList<TorgCentr> randomTorgCentr;
ArrayList<String> torgCentrNames;

Context context;
AutoCompleteTextView searchTorgCentr;
OverlayManager overlayManager;
MapController mapController;
TextView textView;
double longitude;
double latitude;
double itemLongitude;
double itemLatitude;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "MapActivity onCreateView");


View view = (LinearLayout) inflater.inflate(R.layout.map_layout, container, false);
final MapView mapView = (MapView) view.findViewById(R.id.map);
textView = (TextView) view.findViewById(R.id.searchlocation);
searchTorgCentr = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteTextView);

mapView.showBuiltInScreenButtons(true);
mapController = mapView.getMapController();
context = getActivity();
return view;
}

@Override
public void onResume() {
super.onResume();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "MapActivity onCreate");

}

public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "MapActivity onActivityCreated");
context = getActivity();

SetRightMapDisplayAddress rightMapDisplayAddress = new SetRightMapDisplayAddress();
rightMapDisplayAddress.execute(STRING_LOCATION);

DownloadSuperMarketsArray superMarketsArray = new DownloadSuperMarketsArray();
superMarketsArray.execute();

overlayManager = mapController.getOverlayManager();
overlayManager.getMyLocation().setEnabled(false);

super.onActivityCreated(savedInstanceState);
}

第二个 fragment :

public class MainWindowActivity extends Fragment {

private static final String TAG = "MainWindowActivity";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "MainWindowActivity onCreateView");
View view = (RelativeLayout) inflater.inflate(R.layout.main_window_layout, container, false);
if (container == null) {
return null;
}
return view;
}
}

第三个:

public class FindTovarFragment extends Fragment {

private static final String TAG= "FindTovarFragment";

Context context;
ArrayList<Category> categories;
Spinner categoryContainer;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "FindTovarFragment onCreateView");

View view = (LinearLayout) inflater.inflate(R.layout.find_tovar_main_layout, container, false);
categoryContainer = (Spinner) view.findViewById(R.id.category);

return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "FindTovarFragment onActivityCreated");
DownloadCategory downloadCategory = new DownloadCategory();
downloadCategory.execute();
}

MapActivity 的日志:

06-20 11:06:37.709: DEBUG/MapActivity(1290): MapActivity onCreate
06-20 11:06:37.709: DEBUG/MapActivity(1290): MapActivity onCreateView
06-20 11:06:38.509: DEBUG/MapActivity(1290): MapActivity onActivityCreated

一遍又一遍:

06-20 11:07:53.239: DEBUG/MapActivity(1290): MapActivity onCreate
06-20 11:07:53.239: DEBUG/MapActivity(1290): MapActivity onCreateView
06-20 11:07:53.429: DEBUG/MapActivity(1290): MapActivity onActivityCreated
06-20 11:08:23.029: DEBUG/MapActivity(1290): MapActivity onCreate
06-20 11:08:23.039: DEBUG/MapActivity(1290): MapActivity onCreateView
06-20 11:08:23.269: DEBUG/MapActivity(1290): MapActivity onActivityCreated

非常感谢您。

最佳答案

ViewPager 默认在内存中保留 1 页,到当前页的任一侧。因此,在当前页面向左/向右滑动 1 页时,它不会重新创建这些页面。但是当向左/向右滑动超过 1 个页面时,它会再次重新创建这些页面,因此称为 OnCreateView()、OnCreate()。

如果app使用的页面数很少3,可以通过调用增加页面保留数,

mViewPager.setOffscreenPageLimit(2);

描述here

关于android - 为什么会调用fragment的onCreateView、onCreate、onActivityCreated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17203490/

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