gpt4 book ai didi

java - onresume 后 ViewPager 中的 fragment 为空

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:47:24 27 4
gpt4 key购买 nike

我正在编写一个在其 MainActivity 中有 2 个选项卡的应用程序。我使用适用于 Eclipse 的 Android 插件创建了 Activity 模板并对其进行了调整。无论如何,当我在一段时间后恢复应用程序时, fragment 将为空。

这是我的 Activity 代码(我删除了“不重要”的部分)

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;

private static final String ARG_SECTION_NUMBER = "section_number";

/**
* Fragment to display markets on a {@link GoogleMap}.
*/
private GluehweinMapFragment mGluehweinMapFragment;

/**
* Fragment to display available markets as list.
*/
private GluehweinListFragment mGluehweinListFragment;


/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;



/*
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);


initSectionsPagerAdapter();
//[...]

}

private void initSectionsPagerAdapter() {
if (mSectionsPagerAdapter == null || mViewPager == null) {
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the
// three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
break;

}
refreshFragments();
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.getSelectedNavigationIndex();

// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title
// defined by
// the adapter. Also specify this Activity object, which
// implements
// the TabListener interface, as the callback (listener) for
// when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
}



/**
* Hides or shows the closed markets on the list and map
*/
private void updateVisibility() {
if (this.mGluehweinMapFragment != null) {
mGluehweinMapFragment.updateVisibility();
}
if (this.mGluehweinListFragment != null) {
mGluehweinListFragment.updateVisibility();
}
}

@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {

mViewPager.setCurrentItem(tab.getPosition());
invalidateOptionsMenu();
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = null;

Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, position + 1);

switch (position) {
case 0:
// position 0 is the Map
if (mGluehweinMapFragment == null) {
mGluehweinMapFragment = new GluehweinMapFragment();
}
fragment = mGluehweinMapFragment;
break;
case 1:
// position 1 is the List
if (mGluehweinListFragment == null) {
mGluehweinListFragment = new GluehweinListFragment();
}
fragment = mGluehweinListFragment;
break;
}
fragment.setArguments(args);
return fragment;
}

@Override
public int getCount() {
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.tab_map).toUpperCase(l);
case 1:
return getString(R.string.tab_list).toUpperCase(l);
}
return null;
}

}

@Override
protected void onResume() {
// on resume, restart requesting location updates
super.onResume();
//[..]
initSectionsPagerAdapter();
//some calls on the fragments which are null here!!!
refreshFragments();
}



/**
* Notifies the Fragments in the Tabs that data has changed
*/
private void refreshFragments() {
if (this.mGluehweinMapFragment != null) {
mGluehweinMapFragment.updateVisibility();
}
if (this.mGluehweinListFragment != null) {
mGluehweinListFragment.updateVisibility();
}
}

}

我问了一个同事,他说我的Activity 中保留这两个 Fragments 作为成员是不好的,因为 Android 最终会创建一个 Fragments 的新实例 onResume,这就是为什么我的引用为空。但他无法告诉我如何解决我的问题。我正在考虑编写一个 getFragment() 方法,该方法将使用 FragmentManager 来获取 fragment ,但我不知道如何让我的 fragment 处于某个状态,其中我可以使用 FragmentManager 阅读它们。

提前致谢!

最佳答案

您的同事是对的:在您的 Activity 变量中使用对 fragment 的引用不是一个好习惯。您的 Activity 将被破坏,例如当您旋转屏幕时,引用可能会导致内存泄漏。

至于您的问题,将初始化 ViewPager 的代码(您的 initSectionsPagerAdapter() 方法)移动到 onResume。它会在 Activity 首次启动时以及它变得可见时被调用,例如当位于顶部的另一个应用程序关闭时。您不需要 onResume 中的当前代码。

编辑:

当您创建一个 fragment 时,不要将对它的引用存储在 Activity 的变量中。稍后要访问 fragment ,您可以使用:

FragmentManager fm = this.getFragmentManager();
GluehweinMapFragment f1 = (GluehweinMapFragment)fm.getFragments().get(0); // to get one fragment

for (Fragment f : fm.getFragments()) { // to loop through fragments and checking their type
if (f instanceof GluehweinMapFragment) {
}
}

关于java - onresume 后 ViewPager 中的 fragment 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20280811/

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