2014-09-24 56 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. 
}