在导航抽屉片段中保存视图页面片段的状态
Posted
技术标签:
【中文标题】在导航抽屉片段中保存视图页面片段的状态【英文标题】:Save state of viewpager fragment in navigation drawer fragments 【发布时间】:2018-08-01 21:14:56 【问题描述】:我有导航抽屉。在导航抽屉的第一个片段中,有 viewpager,并且有来自网络的信息。Viewpager 也有 2 个片段。当我将片段切换到导航抽屉的设置片段并返回到这个片段时,一切都是消失了..没有任何信息..我怎样才能保存这个片段的状态?或者我可以通过哪种方法发送请求以再次从网络获取信息
主活动
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.motherFragments, FragmentMainPage.newInstance("MainPage"))
.commit();
@Override
public void onBackPressed()
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case android.R.id.home:
drawer.openDrawer(GravityCompat.START);
break;
return super.onOptionsItemSelected(item);
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
Fragment fragment = null;
if (item.isChecked())
drawer.closeDrawer(GravityCompat.START);
return false;
switch (item.getItemId())
case R.id.home:
fragment = new FragmentMainPage().newInstance("Sample");
break;
case R.id.settings:
Intent intent = new Intent(this,SettingsActivity.class);
startActivity(intent);
break;
case R.id.incoming:
fragment = new FragmentIncomingPage().newInstance("Sample");
break;
if (fragment != null)
getSupportFragmentManager()
.beginTransaction().replace(R.id.motherFragments, fragment)
.commit();
item.setChecked(true);
drawer.closeDrawer(GravityCompat.START);
return true;
导航抽屉的Fragment1
public class FragmentMainPage extends Fragment implements
ViewPager.OnPageChangeListener
private Toolbar toolbar;
private MainActivity activity;
private TabLayout tabLayout;
private ViewPager viewPager;
String[] names =
"Sample 1",
"Sample 2"
;
private static FragmentMainPage myInstance;
public static FragmentMainPage newInstance(String testString)
Bundle bundle = new Bundle();
bundle.putString("test",testString);
if(myInstance == null)
myInstance = new FragmentMainPage();
myInstance.setArguments(bundle);
return myInstance;
@Override
public void onAttach(Context context)
super.onAttach(context);
activity = ((MainActivity) context);
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.activity_fragment_main,container,false);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
activity.setSupportActionBar(toolbar);
activity.setTitle(getResources().getText(R.string.bosh_sahifa));
activity.getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewpager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(this);
setupTabName();
return view;
private void setupViewpager(ViewPager viewPager)
MyAdapter adapter = new MyAdapter(getActivity().getSupportFragmentManager());
adapter.addFragement(new Fragment_home1());
adapter.addFragement(new Fragment_home2());
viewPager.setAdapter(adapter);
private void setupTabName()
tabLayout.getTabAt(0).setText(names[0]);
tabLayout.getTabAt(1).setText(names[1]);
@Override
public void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
@Override
public void onPageScrollStateChanged(int state)
private class MyAdapter extends FragmentPagerAdapter
private final List<Fragment> mFragmentList = new ArrayList<>();
public MyAdapter(FragmentManager fm)
super(fm);
@Override
public Fragment getItem(int position)
return mFragmentList.get(position);
@Override
public int getCount()
return mFragmentList.size();
public void addFragement(Fragment fragment)
mFragmentList.add(fragment);
导航抽屉的片段 2
public class FragmentIncomingPage extends Fragment
private Toolbar toolbar;
private MainActivity activity;
private static FragmentIncomingPage myInstance;
public static FragmentIncomingPage newInstance(String testString)
Bundle bundle = new Bundle();
bundle.putString("test",testString);
if(myInstance == null)
myInstance = new FragmentIncomingPage();
myInstance.setArguments(bundle);
return myInstance;
@Override
public void onAttach(Context context)
super.onAttach(context);
activity = ((MainActivity) context);
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.activity_fragment_incoming,container,false);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
activity.setSupportActionBar(toolbar);
activity.setTitle(getResources().getText(R.string.Incoming));
activity.getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return view;
【问题讨论】:
【参考方案1】:有 3 个片段,然后滑动到第 3 个片段然后回到第 1 个片段,第 1 个片段的数据消失了吗?
【讨论】:
如果你的情况是我上面提到的,请尝试ViewPager.setOffscreenPageLimit(2)
在 viewpager 中没有,我可以使用更多片段。它工作正常...当我更改 viewpager 的母片段并回到这个母片段时,一切都消失了..
你能发布一些关于“当我改变 viewpager 的母片段”的代码片段和“回来”的代码片段
所以你打开Fragment1再打开Fragment2,最后再打开Fragment1,fragment1的数据就没了?
是的,确实是这样..viewpager 片段的数据已经消失【参考方案2】:
您可以使用 fragment.addToBackStack(null) 方法。 喜欢,
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, A);
ft.addToBackStack(null);
ft.commit();
【讨论】:
我在哪里使用它?在抽屉片段中或在 MainActivity 中切换抽屉片段。? @JavohirRuzimurodov 当您替换或添加片段时,请使用上述代码。 我照你说的做了..但它仍然丢失数据【参考方案3】:实现它的方法很少。
您可以使用 SharedPreferences 通过 mViewPager.getCurrentItem() 保存当前 Fragment,并在您通过导航抽屉按钮进入 ViewPager 活动时再次设置它。
使用 BaseFragment 并从 Base Fragment 实现您的 Fragment,将 View Pager 的当前可见 Fragment 保留在 Base Fragment 中作为受保护的变量。
作为上面描述的Base Fragment,你可以使用Bas Activity类。
【讨论】:
以上是关于在导航抽屉片段中保存视图页面片段的状态的主要内容,如果未能解决你的问题,请参考以下文章
如何更新导航抽屉中的有状态小部件,同时在 Android 中保持与片段相同的类?