Nesting of Android tabactivity

Source: Internet
Author: User

In this tutorial, I will show you how to use tabactivity activitygroup together for nested activities. This method can have several nested activities without the "lost" option for your tag navigation.
This idea is to use localactivitymanager to start nested activities. Then replace the views and views of activitygroup with different activities.
Now, this may sound complicated, but it is very simple.

First, the tabactivity class.

View plaincopy to clipboardprint?
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. widget. tabhost;

Public class tabactivity extends Android. App. tabactivity {

Public tabhost;

Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. tab_activity_layout );

// Get the tabhost
This. tabhost = gettabhost ();

Tabhost. tabspec spec; // resusable tabspec for each tab
Intent intent; // reusable intent for each tab

// Create an intent to launch the first activity for the tab (to be reused)
Intent = new intent (). setclass (this, firstgroup. Class );

// Initialize a tabspec for the first tab and add it to the tabhost
Spec = tabhost. newtabspec ("firstgroup"). setindicator ("firstgroup ",
Getresources (). getdrawable
(Null) // replace null with R. drawable. your_icon To Set Tab icon
. Setcontent (intent );
Tabhost. addtab (SPEC );

// Create an intent to launch an activity for the tab (to be reused)
Intent = new intent (). setclass (this, secondactivitygroup. Class );

// Initialize a tabspec for the second tab and add it to the tabhost
Spec = tabhost. newtabspec ("secondgroup"). setindicator ("secondgroup ",
Getresources (). getdrawable
(Null) // replace null with R. drawable. your_icon To Set Tab icon
. Setcontent (intent );
Tabhost. addtab (SPEC );

Tabhost. setcurrenttab (0 );
}
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. widget. tabhost;

Public class tabactivity extends Android. App. tabactivity {

Public tabhost;

Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. tab_activity_layout );

// Get the tabhost
This. tabhost = gettabhost ();

Tabhost. tabspec spec; // resusable tabspec for each tab
Intent intent; // reusable intent for each tab

// Create an intent to launch the first activity for the tab (to be reused)
Intent = new intent (). setclass (this, firstgroup. Class );

// Initialize a tabspec for the first tab and add it to the tabhost
Spec = tabhost. newtabspec ("firstgroup"). setindicator ("firstgroup ",
Getresources (). getdrawable
(Null) // replace null with R. drawable. your_icon To Set Tab icon
. Setcontent (intent );
Tabhost. addtab (SPEC );

// Create an intent to launch an activity for the tab (to be reused)
Intent = new intent (). setclass (this, secondactivitygroup. Class );

// Initialize a tabspec for the second tab and add it to the tabhost
Spec = tabhost. newtabspec ("secondgroup"). setindicator ("secondgroup ",
Getresources (). getdrawable
(Null) // replace null with R. drawable. your_icon To Set Tab icon
. Setcontent (intent );
Tabhost. addtab (SPEC );

Tabhost. setcurrenttab (0 );
}

Okay, so this type of activity is all tags. In this example, I just created two labels: "firstgroup" and "secondgroup ". Next, we want to implement activitygroups.
I will only make one of them, because the idea is the same, they both: So let's do the firstgroup. Java class:

View plaincopy to clipboardprint?

Import java. util. arraylist;
Import Android. App. activitygroup;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuinflater;
Import Android. View. menuitem;
Import Android. View. view;

Public class firstgroup extends activitygroup {

// Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
Public static firstgroup group;

// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
Private arraylist <View> history;

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
This. History = new arraylist <View> ();
Group = this;

// Start the root activity withing the group and get its view
View view = getlocalactivitymanager (). startactivity ("citiesactivity", new
Intent (this, citiesactivity. Class)
. Addflags (intent. flag_activity_clear_top ))
. Getdecorview ();

// Replace the view of this activitygroup
Replaceview (View );

}

Public void replaceview (view v ){
// Adds the old one to history
History. Add (v );
// Changes this groups view to the new view.
Setcontentview (v );
}

Public void back (){
If (history. Size ()> 0 ){
History. Remove (history. Size ()-1 );
Setcontentview (history. Get (history. Size ()-1 ));
} Else {
Finish ();
}
}

@ Override
Public void onbackpressed (){
Firstgroup. Group. Back ();
Return;
}

}
</View>
Import java. util. arraylist;
Import Android. App. activitygroup;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuinflater;
Import Android. View. menuitem;
Import Android. View. view;

Public class firstgroup extends activitygroup {

// Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
Public static firstgroup group;

// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
Private arraylist history;

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
This. History = new arraylist ();
Group = this;

// Start the root activity withing the group and get its view
View view = getlocalactivitymanager (). startactivity ("citiesactivity", new
Intent (this, citiesactivity. Class)
. Addflags (intent. flag_activity_clear_top ))
. Getdecorview ();

// Replace the view of this activitygroup
Replaceview (View );

}

Public void replaceview (view v ){
// Adds the old one to history
History. Add (v );
// Changes this groups view to the new view.
Setcontentview (v );
}

Public void back (){
If (history. Size ()> 0 ){
History. Remove (history. Size ()-1 );
Setcontentview (history. Get (history. Size ()-1 ));
} Else {
Finish ();
}
}

@ Override
Public void onbackpressed (){
Firstgroup. Group. Back ();
Return;
}

}

. This is an important part. The code above is to keep this group in a static variable and you can access all nested activities from. This will replace all nested activitygroup activities.
If you do this, you will not loosen the embedded activities in different navigation slots when tab is moved. Now I will tell you how it looks like an activity.

View plaincopy to clipboardprint?
Import Android. App. listactivity;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. arrayadapter;
Import Android. widget. listadapter;
Import Android. widget. listview;

Public class citiesactivity extends listactivity {

// Data to put in the listadapter
Private string [] cities = new string [] {"Bergen", "New York", "Paris "}
Private listadapter adapter;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. cities_activity_layout );
Filldata ();
}

// Fill the list with some data, this can be anything really
Public void filldata (){
Arrayadapter citiesadapter = new arrayadapter (this, R. layout. city_row, cities );
Setlistadapter (citiesadapter );
}

@ Override
Protected void onlistitemclick (listview L, view V, int position, long ID ){
Super. onlistitemclick (L, V, position, ID );

Intent I = new intent (this, showcity. Class );
String city_name = (string) getlistadapter (). getitem (position );
I. putextra ("city_name", city_name );

// Create the view using firstgroup's localactivitymanager
View view = firstgroup. Group. getlocalactivitymanager ()
. Startactivity ("show_city", I
. Addflags (intent. flag_activity_clear_top ))
. Getdecorview ();

// Again, replace the view
Firstgroup. Group. replaceview (View );
}
}
Import Android. App. listactivity;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. arrayadapter;
Import Android. widget. listadapter;
Import Android. widget. listview;

Public class citiesactivity extends listactivity {

// Data to put in the listadapter
Private string [] cities = new string [] {"Bergen", "New York", "Paris "}
Private listadapter adapter;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. cities_activity_layout );
Filldata ();
}

// Fill the list with some data, this can be anything really
Public void filldata (){
Arrayadapter citiesadapter = new arrayadapter (this, R. layout. city_row, cities );
Setlistadapter (citiesadapter );
}

@ Override
Protected void onlistitemclick (listview L, view V, int position, long ID ){
Super. onlistitemclick (L, V, position, ID );

Intent I = new intent (this, showcity. Class );
String city_name = (string) getlistadapter (). getitem (position );
I. putextra ("city_name", city_name );

// Create the view using firstgroup's localactivitymanager
View view = firstgroup. Group. getlocalactivitymanager ()
. Startactivity ("show_city", I
. Addflags (intent. flag_activity_clear_top ))
. Getdecorview ();

// Again, replace the view
Firstgroup. Group. replaceview (View );
}
}

The code above only shows some cities in the list. Then, if you click any entries in this list, it will replace firstgroup's views and perspectives on the activities created by showcity. java. You can do the same thing, showcity and other nested activities.

Related Article

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.