如何在Android中使用Tabs时从子活动返回父活动

时间:2012-01-31 17:27:45

标签: android android-intent android-tabhost android-activity

我有两个标签A,B,带有相应的TabActivityA和TabActivityB。我有第三个ActivityA1,它不在选项卡上,而是来自ActivityA的中间活动。

以下是序列中的代码

 public class AndroidTabLayoutActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();

        // Tab for Home
        TabSpec homeSpec = tabHost.newTabSpec("Home");
        homeSpec.setIndicator("Home", getResources().getDrawable(R.drawable.icon_home_tab));
        Intent homeIntent = new Intent(this, TabActivityA.class);
        homeSpec.setContent(homeIntent);


        // Tab for my cases
        TabSpec helppec = tabHost.newTabSpec("Help");
        // setting Title and Icon for the Tab
        helppec.setIndicator("Help", getResources().getDrawable(R.drawable.icon_cases_tab));
        Intent helpIntent = new Intent(this, TabActivityB.class);
        mycasesspec.setContent(helpIntent);


        // Adding all TabSpec to TabHost
        tabHost.addTab(homeSpec); // Adding home tab
        tabHost.addTab(help); // Adding help tab

    }

ActivityA1 extends Activity
{
}

TabActivityA extends ActivityGroup
{
.....
Intent nextScreen = new Intent(getApplicationContext(), ActivityA1.class);
 View view = getLocalActivityManager().startActivity("ActivityA1",  nextScreen.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)).getDecorView(); 
 setContentView(view);
}

注意:我这样做是因为我想为ActivityA1显示相同的标签

这确实显示了ActivityA1上的标签(Home和Help),但是在单击Home选项卡时,我希望用户转到TabActivityA,但现在它只停留在ActivityA1上。

任何帮助将不胜感激!!

1 个答案:

答案 0 :(得分:0)

好吧我想我明白你现在要问的是,只需保留一个视图的arrayList,在调用SetContentView之前添加到此列表,然后当你准备好回到之前的View do SetContentView(list [count - 1] ]),还要确保删除以前的View并将其丢弃,否则它将永远存在于内存中......