2014-09-24 59 views
1

我在應用程序中創建了三個選項卡,並在每個選項卡內打開了子活動。現在我想要的是我想保存選項卡的狀態。例如如果我正在製作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; 

    } 
    } 
} 
+0

OK,然後......我不理解正確你想達到什麼「現在我想那是什麼,我想保存標籤的狀態,例如,如果我正在使用TAB-1的兒童活動並按TAB-2「。只要我知道,每個活動負責將其內部狀態存儲在onStop上並恢復onStart – eduyayo 2014-09-24 07:30:43

+0

@eduyayo我在每個選項卡中都有多個子活動,所以我希望如果從Tab-1的第二個子活動切換到Tab-2當我回到tab-1時,第二個孩子的活動必須顯示相同的數據,而不是初始的子活動 – user3679400 2014-09-24 09:20:12

+0

so ...用戶點擊tab1,但你仍然顯示activity2? – eduyayo 2014-09-24 09:26:33

回答

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. 
} 

什麼的....

+0

它是否在你的最後。 – user3679400 2014-09-24 11:14:47

+0

我收到錯誤。而在我的末尾將代碼粘貼到我的代碼 – user3679400 2014-09-24 11:19:44

+0

中時,它不工作,因爲我的代碼不是「propper」代碼。可能它甚至不會編譯。順便說一句,注意你試圖通過保持選項卡的內容來混淆你的用戶。 – eduyayo 2014-09-24 12:47:38

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. 
}