我想保存tab的状态

时间:2014-09-24 05:35:19

标签: android tabs android-tabactivity savestate

我在应用程序中创建了三个选项卡,并在每个选项卡中打开子活动。现在我想要的是我想保存标签的状态。例如如果我正在研究TAB-1的儿童活动并按下TAB-2。然后再回到TAB-1。它从相同的子活动而不是初始活动开始。请帮忙

这是我的标签代码。

SuppressWarnings("deprecation")
public class TabHostActivity extends TabActivity implements OnTabChangeListener
{ 
 private static final String[] TABS = { "HomeGroupActivity", "AboutGroupActivity", "ContactGroupActivity" };
 private static final String[] TAB_NAMES = { "Home", "About", "Contact"};
 public static TabHost tabs ;
    public static TabWidget tabWidget ;    
 protected Bitmap roundedImage;
    public boolean checkTabsListener = false;


    public void onCreate(Bundle icicle)
    {        
     super.onCreate(icicle);
        setContentView(R.layout.activity_tab_host);

        Bitmap roundedImage = BitmapFactory.decodeResource(getResources(),R.drawable.ic_tab_background);
        roundedImage = getRoundedCornerBitmap(roundedImage,3);

        tabs = getTabHost();

        tabWidget = tabs.getTabWidget();

     tabs.setOnTabChangedListener(this);

     for (int i = 0; i < TABS.length; i++)
        {
         TabHost.TabSpec tab = tabs.newTabSpec(TABS[i]);

         //Asociating Components
         ComponentName oneActivity = new ComponentName("com.example.tabs", "com.example.tabs." + TABS[i]);
         Intent intent = new Intent().setComponent(oneActivity);           
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         tab.setContent(intent);         
         //Setting the Indicator
         MyTabIndicator myTab = new MyTabIndicator(this, TAB_NAMES[i],(i+1), roundedImage); 
         tab.setIndicator(myTab); 
        tabs.addTab(tab);
        }     

     checkTabsListener = true;

        for(int i=0;i<tabs.getTabWidget().getChildCount();i++)
        {
         tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
        }


  tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background);   

  //Maintaining Clicks

  // Home Tab Click

  tabWidget.getChildAt(0).setOnClickListener(new OnClickListener()
        {   
   @Override
   public void onClick(View v)
   {     
     if(HomeGroupActivity.HomeGroupStack != null && HomeGroupActivity.HomeGroupStack.mIdList.size()>1)
     {      
      HomeGroupActivity.HomeGroupStack.getLocalActivityManager().removeAllActivities();
      HomeGroupActivity.HomeGroupStack.mIdList.clear();
      HomeGroupActivity.HomeGroupStack.mIntents.clear();
      HomeGroupActivity.HomeGroupStack.mAnimator.removeAllViews();
      HomeGroupActivity.HomeGroupStack.startChildActivity("CareGroupActivity", new Intent(HomeGroupActivity.HomeGroupStack, HomeActivity.class));

     }

     tabWidget.setCurrentTab(0);
     tabs.setCurrentTab(0);
     tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background);  
   }
        });


  // About tab Click

  tabWidget.getChildAt(1).setOnClickListener(new OnClickListener()
        {   
   public void onClick(View v)
   {     
    if(AboutGroupActivity.AboutGroupStack != null && AboutGroupActivity.AboutGroupStack.mIdList.size()>0)
    {
     AboutGroupActivity.AboutGroupStack.getLocalActivityManager().removeAllActivities();
     AboutGroupActivity.AboutGroupStack.mIdList.clear();      
     AboutGroupActivity.AboutGroupStack.mIntents.clear();
     AboutGroupActivity.AboutGroupStack.mAnimator.removeAllViews();            
     AboutGroupActivity.AboutGroupStack.startChildActivity("TrackingGroupActivity", new Intent(AboutGroupActivity.AboutGroupStack, About.class));           
    }     

    tabWidget.setCurrentTab(1);
    tabs.setCurrentTab(1);
    tabs.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_tab_background);       
   }
        });

  // Contact tab click

  tabWidget.getChildAt(2).setOnClickListener(new OnClickListener()
        {   
   public void onClick(View v)
   {     
    if(ContactGroupActivity.ContactGroupStack != null && ContactGroupActivity.ContactGroupStack.mIdList.size()>0)
    {

     ContactGroupActivity.ContactGroupStack.getLocalActivityManager().removeAllActivities();
     ContactGroupActivity.ContactGroupStack.mIdList.clear();      
     ContactGroupActivity.ContactGroupStack.mIntents.clear();
     ContactGroupActivity.ContactGroupStack.mAnimator.removeAllViews();            
     ContactGroupActivity.ContactGroupStack.startChildActivity("DashboardGroupActivity", 
             new Intent(ContactGroupActivity.ContactGroupStack, Contact.class));   
    }     

    tabWidget.setCurrentTab(2);
    tabs.setCurrentTab(2);
    tabs.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_tab_background);       
   }
        });


    }


    public class MyTabIndicator extends LinearLayout 
    {
  public MyTabIndicator(Context context, String label, int tabId, Bitmap bgImg)
  {
   super(context);
   LinearLayout tab = null;
   TextView tv;
   this.setGravity(Gravity.CENTER);

   if(tabId == 1)
   {
    tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.home_tab, null);
    tv = (TextView)tab.findViewById(R.id.tab_label);
    tv.setText(label);
   }



   else if(tabId == 2)
   {
    tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.about_tab, null);
    tv = (TextView)tab.findViewById(R.id.tab_label);
    tv.setText(label);
   }
   else if(tabId == 3)
   {
    tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contact_tab, null);
    tv = (TextView)tab.findViewById(R.id.tab_label);
    tv.setText(label);
   }

   this.addView(tab, new LinearLayout.LayoutParams(320/4,55));   
  }  
    }



 public void onTabChanged(String tabId) 
 {      
  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(tabs.getApplicationWindowToken(), 0);

        for(int i=0; i<tabs.getTabWidget().getChildCount(); i++)
  {                             
         if(tabId.equalsIgnoreCase(TABS[i]))
   {            
    tabs.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_tab_background);     
   }
   else
   {
    tabs.getTabWidget().getChildAt(i).setBackgroundColor((Color.TRANSPARENT));
   }     
    }  
 }

  public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPxRadius)
 {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx =roundPxRadius;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
 }

 public void onResume()
 {
  super.onResume();


  //ReConstructing TabViews
  reDesignTabViews(); 
 }

 public void onPause()
 {
  super.onPause();     
 }




 /**
  * Method used to re constructing the Views at tab bar. This solves tabs disappearing issue.
  */
 public void reDesignTabViews()
 {
  MyTabIndicator myIndicator;


  //Construction of tab views....
  for(int i=0 ; i< tabWidget.getChildCount() ; i++)
  {
   myIndicator = (MyTabIndicator) tabWidget.getChildAt(i);
   myIndicator.removeAllViews();

   switch (i) 
   {

     case 0:
     myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.home_tab, null));
     break;
    case 1:    
     myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.about_tab, null));    
     break;
    case 2:    
     myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.contact_tab, null));    
     break;

   }   
  }  
 }

2 个答案:

答案 0 :(得分:1)

public void onTabChanged(String tabId) {      
  tabs.clearAllTabs();
  for(int i = 0; i < TABS.length; i++) {
    //Add here same code you´ve got up there but with the same intent for each tabSpec
  }
  setCurrentTabByTag (tabId); // OR, if you know the id, select current by id.
}

答案 1 :(得分:0)

public void onTabChanged(String tabId) {      
  tabs.clearAllTabs();
  for(int i = 0; i < TABS.length; i++) {
    //Add here same code you´ve got up there but with the same intent for each tabSpec
  }
  setCurrentTabByTag (tabId); // OR, if you know the id, select current by id.
}

或其他......

相关问题