我正在尝试开发一个应用程序,其要求是在关闭应用程序后保留Tab,或突然如果应用程序在运行时被销毁
我的应用有3个标签,我在第二个标签中添加商品" CheckList" 当我关闭我的应用程序第二个标签时,再次使用新页面而不是之前添加的项目
即使关闭了应用程序,我希望我的应用程序与图片2相同,但似乎每次关闭应用程序选项卡时都会显示为图片1
图片1 http://i.stack.imgur.com/KSgTZ.png
图片2 http://i.stack.imgur.com/oUim4.png
Psuedo代码:
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开发新手,请尽量帮助解决此问题
答案 0 :(得分:0)
您无法保留Fragment
个实例。
相反,你应该坚持和恢复Fragment
的状态。 (例如,使用SharedPreferences
)