【发布时间】:2016-07-26 00:43:07
【问题描述】:
我正在尝试开发一个应用程序,其要求是即使在关闭应用程序后或在应用程序在运行时突然被破坏时仍保留选项卡
我的应用有 3 个标签,我在 secondtab “CheckList”中添加项目 当我关闭我的应用第二个标签时,它会再次使用新页面而不是之前添加的项目
我希望我的应用即使在关闭应用程序后也与图 2 相同,但似乎每次我关闭应用程序选项卡时都像图 1 一样
图1http://i.stack.imgur.com/KSgTZ.png
图2http://i.stack.imgur.com/oUim4.png
伪代码:
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public Fragment fragment;
private FragmentActivity myContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getSupportActionBar();
View viewActionBar = getLayoutInflater().inflate(R.layout.titlebar, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT
);
TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.mytext);
textviewTitle.setText("Application");
actionBar.setCustomView(viewActionBar, params);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(2);
// 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));
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch(position){
case 0:
fragment = new FirstTab();
break;
case 1:
fragment = new SecondTab();
break;
case 2:
fragment = new ThirdTab();
}
return fragment;
}
第二个标签代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if(view==null) {
view = inflater.inflate(R.layout.tab2, container, false);
// retain this fragment
bt = (ImageView) view.findViewById(R.id.imageview3);
tl = (TableLayout) view.findViewById(R.id.table);
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
我卡在这里,你能告诉我如何用上述方法保存这个标签和 如何留住?
我看到很少有与保留片段相关的答案,但它并没有解决我的问题
我是android开发新手,请帮忙解决这个问题
【问题讨论】: