Android Activitygroup implemented Tabhost keep activity in its original state

Source: Internet
Author: User

When Tabhost is implemented with Activitygroup, the activity in TAB1 is reloaded when switching from TAB1 to TAB2 and back

public class Mainuiactivity extends activitygroup{private Context context=this; private tabhost mtabhost; private String Tab1= "Home"; Private String tab2= "collection"; Private String tab3= "3"; Private String tab4= "4"; Private String tab5= "more"; Define an array to hold the button picture private int mimageviewarray[] = {R.drawable.tab_home_btn,r.drawable.tab_message_btn,r.drawable.tab_ SELFINFO_BTN, R.drawable.tab_square_btn,r.drawable.tab_more_btn};//tab tab text Private String mtextviewarray[] = {TAB1 , TAB2, TAB3, TAB4, TAB5}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stubs Uper.oncreate (savedinstancestate); Setcontentview (R.layout.tab_main); mtabhost= (tabhost) findViewById ( R.ID.TABHOST1); Mtabhost.setup (); Mtabhost.setup (This.getlocalactivitymanager ()); Intent i1=new Intent (This, Mainactivity.class); Intent i_collect=new Intent (this,collectactivity.class);//intent i1=new Intent (This, Mainactivity.class);//intent i1=new Intent (this,mainactivity.class);//intent i1=new Intent (this,mainactiviTy.class) Mtabhost.addtab (Mtabhost.newtabspec (TAB1) setindicator (Gettabitemview (0)). SetContent (R.id.zhuye). SetContent (I1));///Note that this is Newtabspec (TAB2), each tab tab is written differently to save the original state Mtabhost.addtab (MTABHOST.NEWTABSPEC). Setindicator (Gettabitemview (1)). SetContent (R.id.zhuye2). SetContent (I_collect)); Mtabhost.addtab ( Mtabhost.newtabspec (TAB1). Setindicator (Gettabitemview (2)). SetContent (R.id.zhuye3). SetContent (I1)); Mtabhost.addtab (Mtabhost.newtabspec (TAB1). Setindicator (Gettabitemview (3)). SetContent (R.id.zhuye4). SetContent ( I1)); Mtabhost.addtab (Mtabhost.newtabspec (TAB1) Setindicator (Gettabitemview (4)). SetContent (R.id.zhuye5). SetContent (I1));} /** * give tab button settings icon and text */private view Gettabitemview (int index) {View view = Layoutinflater.from (context). Inflate ( R.layout.tab_item, NULL); ImageView ImageView = (ImageView) View.findviewbyid (R.id.tab_icon); Imageview.setimageresource (Mimageviewarray[index]); TextView TextView = (TextView) View.findviewbyid (r.id.tab_name); Textview.settext (Mtextviewarray[index]); return View;}        public static Intent Createintent (context context) {Intent i = new Intent (context, mainactivity.class);        I.addflags (Intent.flag_activity_clear_top);    return i; }}
The workaround is already written in the code above, which is to set a different tag for each tab tab

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.