如何在启动其他活动时保持Tabhost

时间:2011-09-21 08:51:38

标签: android android-tabhost

TabHost有问题......

我有TabBarActivity类流程:

public class TabBarActivity extends TabActivity实现了OnTabChangeListener {

/*
 * (non-Javadoc)
 * 
 * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.layout_tab);

    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);

    TabSpec mTab1 = tabHost.newTabSpec("tab1");
    TabSpec mTab2 = tabHost.newTabSpec("tab2");

    mTab1.setIndicator("TAB1").setContent(
            new Intent(this, TAB1.class));
    mTab2.setIndicator("TAB2").setContent(
            new Intent(this, TAB2.class));

    /** Add tabSpec to the TabHost to display. */
    tabHost.addTab(mTab1);
    tabHost.addTab(mTab2);


}

我在TAB1.java类中有ListView(例如Contact名称)

假设: 我有一个屏幕布局,底部有2个标签(TAB1,TAB2)。当我单击TAB1时,我有一个联系人姓名列表,如果我点击itemList。 ItemList的详细信息屏幕将打开。在“DetailScreen”布局中,我想在底部屏幕上保留(TAB1,TAB2)tabhost。

现在,当我运行一个应用程序并单击itemList时。我没有在每个项目列表的DetailScreen中保留tabhost(TAB1,TAB2)...

如何在启动项目列表的活动时保留它(例如:DetailScreen.java)

谢谢你


@ all: 感谢您的反馈,但似乎您不理解我的问题。

我的问题: 详述我的项目: 1-我有一些课程: + TabBarActivity.java(源代码流程如上,首页,底部布局Tab1上有2个选项卡,Tab2 ....运行应用程序时默认forcus Tab1) + Tab1.java(重要:在此课程中,我有listview(例如手机上的youtube),当我点击列表中的一个项目时 - >打开每个List项目的详细信息屏幕.. 。 好 ) + Tab2.java(做点什么......)

2 - 当我运行应用时,您会看到 2 Tab底部布局和默认的startActivity Tab1.java - >然后在这个屏幕上显示listview ... 点击列表中的一项 - >打开此项目的详细信息屏幕(StartActivity DetailItem.java) - > 但是底部的2个标签将会消失 ....

我不知道为什么????

请帮帮我:((

3 个答案:

答案 0 :(得分:0)

您可以使用此自定义类实现此目的:

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;

/*
 * The purpose of this Activity is to manage the activities in a tab.
 * Note: Child Activities can handle Key Presses before they are seen here.
 * @author Eric Harlow
 */
public class TabGroupActivity extends ActivityGroup {

    private ArrayList<String> mIdList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       
        if (mIdList == null) mIdList = new ArrayList<String>();
    }

    /*
     * This is called when a child activity of this one calls its finish method. 
     * This implementation calls {@link LocalActivityManager#destroyActivity} on the child activity
     * and starts the previous activity.
     * If the last child activity just called finish(),this activity (the parent),
     * calls finish to finish the entire group.
     */
  @Override
  public void finishFromChild(Activity child) {
      LocalActivityManager manager = getLocalActivityManager();
      int index = mIdList.size()-1;

      if (index < 1) {
              finish();
              return;
          }

          manager.destroyActivity(mIdList.get(index), true);
          mIdList.remove(index);
          index--;
          String lastId = mIdList.get(index);
          Intent lastIntent = manager.getActivity(lastId).getIntent();
          Window newWindow = manager.startActivity(lastId, lastIntent);
          setContentView(newWindow.getDecorView());
  }

  /*
   * Starts an Activity as a child Activity to this.
   * @param Id Unique identifier of the activity to be started.
   * @param intent The Intent describing the activity to be started.
   * @throws android.content.ActivityNotFoundException.
   */
  public void startChildActivity(String Id, Intent intent) {     
      Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
      if (window != null) {
          mIdList.add(Id);
          setContentView(window.getDecorView()); 
      }    
  }

  /*
   * The primary purpose is to prevent systems before android.os.Build.VERSION_CODES.ECLAIR
   * from calling their default KeyEvent.KEYCODE_BACK during onKeyDown.
   */
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
          return true;
      }
      return super.onKeyDown(keyCode, event);
  }

  /*
   * Overrides the default implementation for KeyEvent.KEYCODE_BACK 
   * so that all systems call onBackPressed().
   */
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          onBackPressed();
          return true;
      }
      return super.onKeyUp(keyCode, event);
  }

  /*
   * If a Child Activity handles KeyEvent.KEYCODE_BACK.
   * Simply override and add this method.
   */
  @Override
  public void  onBackPressed  () {
      int length = mIdList.size();
      if ( length >=1) {
          Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
          current.finish();
      }
  }
}

使用这样:

在包含标签的主类中:

public class MainActivity extends TabGroupActivity {

}

在你的onItemClickListener中你可以像这样开始活动:

startChildActivity("CollectionList", new Intent(this,CollectionMenu.class));

当您在CollectionMenu(扩展TabGroupActivity)时,您可以像下面的代码一样开始您的子活动:

Intent previewMessage = new Intent(getParent(), DetailScreen.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("DetailScreen", previewMessage);

这应该有用。如果你有任何问题,请问!

答案 1 :(得分:0)

您必须使用TabGroupActivity类

你必须进一步实施 Tab1Activity类和Tab2Activity类 它们是从TabGroupActivity

扩展而来的

你将分别调用startChildActivity(TAB1.class)和startChildActivity(TAB2.class), 通过这个,你可以选择你的标签,无论你是在TAB1还是TAB2。

你可以在线实现TabGroupActivity类实现的样本,

最近我有一个类似于此的simillar项目。

答案 2 :(得分:0)

使用方法:

在某些标签(例如4个标签)的一个标签中,每个标签都会延伸TabActivity,您可以使用方法startChildActivity开始真正的活动。

public class Tab_BookCityActivity extends TabGroupActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startChildActivity("BookCityActivity", new Intent(this, BookCityActivity.class));

    }
}