Android TabHost在锁定手机并重新打开后消失:
我的Android TabHost在锁定手机并重新打开后消失:,android,android-viewpager,android-tabhost,Android,Android Viewpager,Android Tabhost,我的FragmentActivity中的TabHost有一个奇怪的问题,它包含一个ViewPager 问题是,当我在使用应用程序时关闭手机(按下电源按钮),然后我将手机调回原位,应用程序重新打开时,此时我的TabHost丢失。因此,关闭手机会导致TabHost消失 我的猜测是,我需要将我的tabHost状态保存在saveInstanceState对象中,然后在onResume中恢复它,我只是不知道它是如何完成的。这是我的片段活动的代码: public class TabsViewPagerFra
FragmentActivity
中的TabHost
有一个奇怪的问题,它包含一个ViewPager
问题是,当我在使用应用程序时关闭手机(按下电源按钮),然后我将手机调回原位,应用程序重新打开时,此时我的TabHost
丢失。因此,关闭手机会导致TabHost
消失
我的猜测是,我需要将我的tabHost
状态保存在saveInstanceState
对象中,然后在onResume
中恢复它,我只是不知道它是如何完成的。这是我的片段活动的代码
:
public class TabsViewPagerFragmentActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener
{
static final String TAG = TabsViewPagerFragmentActivity.class.getSimpleName();
private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo;
public ViewPagerAdapter mPagerAdapter;
private TextView tvReportName, tvTabTitle;
private Button bBackToParameters;
private Dialog progressDialog;
private SGRaportManagerAppObj application;
private int numberOfTabs = 0;
private Display display;
public static final int POPUP_MARGIN = 6;
LeftSideMenu leftSideMenu;
public void NotifyTabActivityViewPagerAdapter()
{
mPagerAdapter.notifyDataSetChanged();
}
public ViewPagerAdapter getTabActivityViewPagerAdapter()
{
return mPagerAdapter;
}
public ViewPager getTabActivityViewPager()
{
return mViewPager;
}
public void setCurrentTabTitle (String title)
{
tvTabTitle.setText(title);
Log.d(TAG, "set tab title from activity: "+title);
}
/**
* Maintains extrinsic info of a tab's construct
*/
private class TabInfo
{
private String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class<?> clazz, Bundle args)
{
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
/**
* A simple factory that returns dummy views to the Tabhost
*/
class TabFactory implements TabContentFactory {
private final Context mContext;
/**
* @param context
*/
public TabFactory(Context context) {
mContext = context;
}
/** (non-Javadoc)
* @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
/** (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
application = SGRaportManagerAppObj.getInstance();
display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// Inflate the layout
setContentView(R.layout.tabs_screen_activity_layout);
tvTabTitle = (TextView) findViewById(R.id.tvTabName);
tvReportName = (TextView)findViewById(R.id.tvReportName);
tvReportName.setText(application.currentReport.getName()+ " - ");
bBackToParameters = (Button) findViewById(R.id.bBackToParameters);
leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);
applyOnClickListenerToLeftSideMenu();
findViewById(R.id.showLeftMenuButton).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
View panel = findViewById(R.id.leftSideMenu);
View appPanel = findViewById(R.id.appLayout);
if (panel.getVisibility() == View.GONE){
appPanel.setLayoutParams(new LinearLayout.LayoutParams(width, LayoutParams.FILL_PARENT));
panel.setVisibility(View.VISIBLE);
applyOnClickListenerToLeftSideMenu();
}else{
ToggleButton button = (ToggleButton) findViewById(R.id.showLeftMenuButton);
button.setChecked(false);
panel.setVisibility(View.GONE);
}
}
});
// Initialise the TabHost
progressDialog = DialogUtils.createProgressDialog(this, this.getString(R.string.populating_view_pager));
progressDialog.show();
if (SGRaportManagerAppObj.getInstance().parametersRepository.getParametersRepository().size() == 0)
{
bBackToParameters.setText(R.string.back_to_report_list);
}
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
// Intialise ViewPager
this.intialiseViewPager();
progressDialog.dismiss();
}
/** (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
super.onSaveInstanceState(outState);
}
/**
* Initialise ViewPager
*/
public void intialiseViewPager()
{
List<Fragment> fragments = new Vector<Fragment>();
// TabInfo tabInfo = null;
if (application.getCurrentDataSource().equals(DataSource.SSRS))
{
numberOfTabs = application.currentReport.getTabsList().size();
}
else if (application.getCurrentDataSource().equals(DataSource.SGRDL))
{
numberOfTabs = application.currentReport.getODTabsList().size();
Log.d(TAG, "CURRENT REPORT FROM VIEW PAGER: "+ application.currentReport.toString());
}
Log.d(TAG,"Current Tabs number from TabsViewPager activity: " +numberOfTabs);
if (application.getCurrentDataSource().equals(DataSource.SSRS))
{
for (int i = 0; i < numberOfTabs; i++)
{
Tab tempTab = application.currentReport.getTabsList().get(i);
if (tempTab.getTabTemplateId() == 7)
{
GridFragment gridFragment = new GridFragment(tempTab);
fragments.add(gridFragment);
}
else if (tempTab.getTabTemplateId() == 8)
{
NewChartFragment chartFragment = new NewChartFragment(tempTab, this);
fragments.add(chartFragment);
}
}
}
else if (application.getCurrentDataSource().equals(DataSource.SGRDL))
{
for (int i = 0; i < numberOfTabs; i++)
{
ODTab tempTab = application.currentReport.getODTabsList().get(i);
if (tempTab.getTabType().equals(ODGrid.XML_GRID_ELEMENT))
{
GridFragment gridFragment = GridFragment.newInstance(tempTab.getTabId());
fragments.add(gridFragment);
}
else if (tempTab.getTabType().equals(ODChart.XML_CHART_ELEMENT))
{
NewChartFragment chartFragment = NewChartFragment.newInstance(tempTab.getTabId());
fragments.add(chartFragment);
}
}
}
Log.d(TAG, "Current report fragments set to adapter: "+fragments.toString());
/*
if (this.mPagerAdapter == null)
{
this.mPagerAdapter = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);
}
else
{
this.mPagerAdapter.removeAllFragments();
this.mPagerAdapter.addFragmentsListToAdapter(fragments);
}
*/
this.mPagerAdapter = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments);
this.mViewPager = (ViewPager)super.findViewById(R.id.pager);
// this.mViewPager.setAdapter(null);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOffscreenPageLimit(0);
this.mViewPager.setOnPageChangeListener(this);
Log.d(TAG, "Adapter initialized!");
}
/**
* Initialise the Tab Host
*/
public void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
/*
//new edit
if (mTabHost.getChildCount() > 0)
{
mTabHost.removeAllViews();
}
*/
mTabHost.setup();
TabInfo tabInfo = null;
mapTabInfo = new HashMap<String, TabsViewPagerFragmentActivity.TabInfo>();
if (args != null)
{}
else
{
if (application.getCurrentDataSource().equals(DataSource.SSRS))
{
int numberOfTabs = application.currentReport.getTabsList().size();
for (int i = 0; i < numberOfTabs; i++)
{
Tab tempTab = application.currentReport.getTabsList().get(i);
if (tempTab.getTabTemplateId() == 7)
{
//GridFragment gridFragment = new GridFragment(tempTab);
TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab "+String.valueOf(i)).setIndicator("Tab "+String.valueOf(i)), ( tabInfo = new TabInfo("Tab "+String.valueOf(i), GridFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
}
else if (tempTab.getTabTemplateId() == 8)
{
TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab "+String.valueOf(i)).setIndicator("Tab "+String.valueOf(i)), ( tabInfo = new TabInfo("Tab "+String.valueOf(i), NewChartFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
}
}
}
else if (application.getCurrentDataSource().equals(DataSource.SGRDL))
{
int numberOfTabs = application.currentReport.getODTabsList().size();
for (int i = 0; i < numberOfTabs; i++)
{
ODTab tempTab = application.currentReport.getODTabsList().get(i);
// Log.d(TAG,"Crashed Tab type: "+ tempTab.getTabType());
if (tempTab.getTabType().equals(ODGrid.XML_GRID_ELEMENT))
{
//GridFragment gridFragment = new GridFragment(tempTab);
TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab "+String.valueOf(i)).setIndicator("Tab "+String.valueOf(i)), ( tabInfo = new TabInfo("Tab "+String.valueOf(i), GridFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
}
else if (tempTab.getTabType().equals(ODChart.XML_CHART_ELEMENT))
{
TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab "+String.valueOf(i)).setIndicator("Tab "+String.valueOf(i)), ( tabInfo = new TabInfo("Tab "+String.valueOf(i), NewChartFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
}
}
}
}
// Default to first tab
//this.onTabChanged("Tab1");
//
mTabHost.setOnTabChangedListener(this);
}
/**
* Add Tab content to the Tabhost
* @param activity
* @param tabHost
* @param tabSpec
* @param clss
* @param args
*/
private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo)
{
// Attach a Tab view factory to the spec
ImageView indicator = new ImageView(activity.getBaseContext());
indicator.setPadding(10, 10, 10, 10);
indicator.setImageResource(R.drawable.tab_select_icon_selector);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10, 10);
indicator.setLayoutParams(lp);
tabSpec.setIndicator(indicator);
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}
/** (non-Javadoc)
* @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
*/
public void onTabChanged(String tag) {
//TabInfo newTab = this.mapTabInfo.get(tag);
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int, float, int)
*/
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
*/
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
this.mTabHost.setCurrentTab(position);
}
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
*/
@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
public类TabsViewPagerFragmentActivity扩展FragmentActivity实现ViewPager.OnPageChangeListener、TabHost.OnTabChangeListener
{
静态最终字符串标记=TabsViewPagerFragmentActivity.class.getSimpleName();
私有TabHost-mTabHost;
私有视图寻呼机mViewPager;
私有HashMapMapTabInfo;
公共视图页面雷达设备mPagerAdapter;
私有文本视图tvReportName、tvTabTitle;
专用按钮bBackToParameters;
私人对话;
专用SGRaportManagerAppObj应用程序;
私有int numberOfTabs=0;
私人显示器;
公共静态最终整数/单位边距=6;
LeftSideMenu LeftSideMenu;
public void NotifyTabActivityViewPagerAdapter()
{
mpageAdapter.notifyDataSetChanged();
}
public ViewPagerAdapter getTabActivityViewPagerAdapter()
{
返回自适应;
}
公共ViewPager GetTableActivityViewPager()
{
返回mviewpage;
}
public void setCurrentTabTitle(字符串标题)
{
tvTabTitle.setText(标题);
Log.d(标记,“设置活动的选项卡标题:”+标题);
}
/**
*维护选项卡构造的外部信息
*/
私有类TabInfo
{
私有字符串标签;
私家级CLS;
私有包args;
私有片段;
TabInfo(字符串标记、类clazz、包参数)
{
this.tag=tag;
this.clss=clazz;
this.args=args;
}
}
/**
*将虚拟视图返回到Tabhost的简单工厂
*/
类TabFactory实现TabContentFactory{
私有最终上下文mContext;
/**
*@param上下文
*/
公共选项卡工厂(上下文){
mContext=上下文;
}
/**(非Javadoc)
*@see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
公共视图createTabContent(字符串标记){
视图v=新视图(mContext);
v、 设置最小宽度(0);
v、 设置最小高度(0);
返回v;
}
}
/**(非Javadoc)
*@see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
application=SGRaportManagerAppObj.getInstance();
显示=((WindowManager)getSystemService(Context.WINDOW_服务)).getDefaultDisplay();
//使布局膨胀
setContentView(R.layout.tabs\u screen\u activity\u layout);
tvTabTitle=(TextView)findViewById(R.id.tvTabName);
tvReportName=(TextView)findViewById(R.id.tvReportName);
tvReportName.setText(application.currentReport.getName()+“-”);
bbacktopParameters=(按钮)findViewById(R.id.bbacktopParameters);
leftSideMenu=(leftSideMenu)findViewById(R.id.leftSideMenu);
applyOnClickListenerToLeftSideMenu();
findViewById(R.id.showLeftMenuButton).setOnClickListener(新视图.OnClickListener())
{
@凌驾
公共void onClick(视图v)
{
显示d=((WindowManager)getSystemService(Context.WINDOW_服务)).getDefaultDisplay();
int width=d.getWidth();
视图面板=findViewById(R.id.leftSideMenu);
视图appPanel=findViewById(R.id.appLayout);
if(panel.getVisibility()==View.GONE){
appPanel.setLayoutParams(新的LinearLayout.LayoutParams(宽度,LayoutParams.FILL_父项));
panel.setVisibility(View.VISIBLE);
applyOnClickListenerToLeftSideMenu();
}否则{
ToggleButton=(ToggleButton)findViewById(R.id.showLeftMenuButton);
按钮。setChecked(假);
panel.setVisibility(View.GONE);
}
}
});
//初始化TabHost
progressDialog=DialogUtils.createProgressDialog(this,this.getString(R.string.populating_view_pager));
progressDialog.show();
如果(SGRaportManagerAppObj.getInstance().parametersRepository.getParametersRepository().size()==0)
{
bBackToParameters.setText(R.string.back\u to\u report\u list);
}
this.initialiseTabHost(savedInstanceState);
如果(savedInstanceState!=null){
mTabHost.setCurrentTabByTag(savedInstanceState.getString(“tab”);//根据保存的状态设置选项卡
}
//初始化ViewPager
这个.initialieviewpage();
progressDialog.disclose();
}
/**(非Javadoc)
*@see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/
SaveInstanceState上受保护的无效(束超出状态){
outState.putString(“tab”,mTabHost.getCurrentTabTag());//保存选定的选项卡
super.onSaveInstanceState(超出状态);
}
/**
*初始化视图寻呼机
*/
public void initialiseviewpage()
{
列表片段=新向量();
//TabInfo TabInfo=null;
if(application.getCurrentDataSource().equals(DataSource.SSRS))
{
numberOfTabs=application.currentReport.GetTabList().size();
}
else if(application.getCurrentDataSource().equals(DataSource.SGRDL))
{
numberOfTabs=application.currentReport.getODTabsList().size();
Log.d(标记“来自视图页面的当前报告:”+application.currentReport.toString());
}
Log.d(标签,“来自TabsViewPage活动的当前选项卡编号:”+numberOfTabs);
if(application.getCurrentDataSource().equals(DataSource.SSRS))
{
用于(int i)=
@Override
protected void onResume() {
super.onResume();
if (mTabHost == null)
{
this.initialiseTabHost(null);
}
}
package com.example.tabhost;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TextView;
public class TabsViewPagerFragmentActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener
{
static final String TAG = TabsViewPagerFragmentActivity.class.getSimpleName();
private TabHost mTabHost; // TabHost? Why not a FragmentTabHost?
private ViewPager mViewPager;
private TextView tvTabTitle;
private Display display;
public static final int POPUP_MARGIN = 6;
public void NotifyTabActivityViewPagerAdapter() {}
public boolean getTabActivityViewPagerAdapter()
{
return true;
}
public ViewPager getTabActivityViewPager()
{
return mViewPager;
}
public void setCurrentTabTitle (String title)
{
tvTabTitle.setText(title);
Log.d(TAG, "set tab title from activity: "+title);
}
private class TabInfo
{
private String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class<?> clazz, Bundle args)
{
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
class TabFactory implements TabContentFactory {
private final Context mContext;
public TabFactory(Context context) {
mContext = context;
}
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// Inflate the layout
setContentView(R.layout.activity_main);
// Initialise the TabHost
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
}
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
super.onSaveInstanceState(outState);
}
public void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
/*
* Why not use a FragmentTabHost and setup (Context context, FragmentManager manager, int containerId)
* see: http://getquery.com/android-fragment-tab-example/
*/
mTabHost.setup();
int numberOfTabs = 3;//application.currentReport.getTabsList().size();
for (int i = 0; i < numberOfTabs; i++)
{
String str = "Tab " + String.valueOf(i);
TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec(str).setIndicator(str), null);
}
mTabHost.setOnTabChangedListener(this);
}
private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo)
{
// Attach a Tab view factory to the spec
ImageView indicator = new ImageView(activity.getBaseContext());
indicator.setPadding(10, 10, 10, 10);
indicator.setImageResource(R.drawable.ic_launcher);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10, 10);
indicator.setLayoutParams(lp);
tabSpec.setIndicator(indicator);
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}
@Override
public void onStart() {
super.onStart();
Log.e("onStart", "mTabHost = " + mTabHost);
}
public void onTabChanged(String tag) {
//TabInfo newTab = this.mapTabInfo.get(tag);
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
this.mTabHost.setCurrentTab(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>