簡體   English   中英

活動和子活動

[英]Activities and sub activities

我的應用程序有5種不同的活動(將它們命名為A,B,C,D,E),並且有一個菜單(由屏幕底部的按鈕制作),可以在A,B ...... E之間切換

當我顯示A時,有一些按鈕,用戶通過該按鈕進入A頂部的另一個活動A1,然后從那里進入A12,所以我的活動堆棧類似於A> A1> A12,現在用戶點擊B的菜單按鈕,然后他從那里繼續B1。 (如果用戶按下按鈕切換回A,則堆棧將類似於A> A1> A12> B> B1),因此它應顯示A12,因為它位於A的頂部。

我該如何管理這個scanario? 我沒有任何暗示

使用TabHost和ActivityGroup在選定的選項卡下顯示多個活動,而不是屏幕底部的按鈕。

<TabHost
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
      android:id="@android:id/tabcontent"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" />

    <TabWidget
      android:id="@android:id/tabs"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="0" />

  </LinearLayout>

</TabHost>

主要活動:

/**
 * Activity that displays the main tabs and manages separate activity for the
 * selected tab.
 */
public class MainActivity extends TabActivity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    // load main view
    setContentView(R.layout.main_activity);

    // get the TabHost
    TabHost tabHost = getTabHost();

    // resusable TabSpec for each tab
    TabHost.TabSpec spec;

    // reusable Intent for each tab
    Intent intent;

    // first tab
    intent = new Intent(this, TabGroupActivity.class);
    spec = tabHost.newTabSpec("first")
        .setIndicator(createTabIndicatorView(tabHost, "First", R.drawable.ic_tab_first))
        .setContent(intent);
    tabHost.addTab(spec);

    // second tab
    intent = new Intent(this, TabGroupActivity.class);
    spec = tabHost.newTabSpec("second")
        .setIndicator(createTabIndicatorView(tabHost, "Second", R.drawable.ic_tab_second))
        .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
  }

  /**
   * Creates tabs with custom layout.
   * 
   * @param tabHost the tab host
   * @param tabTitle the title of the tab
   * @param icon the icon of the tab
   * @return the view representing single tab
   */
  private View createTabIndicatorView(TabHost tabHost, CharSequence tabTitle, int icon) {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View tabIndicator = inflater.inflate(R.layout.tab_indicator, tabHost, false);
    final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
    tv.setText(tabTitle);
    final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon);
    iconView.setImageDrawable(getResources().getDrawable(icon));
    return tabIndicator;
  }

}

活動組:

/**
 * 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.
 */
public class TabGroupActivity extends ActivityGroup {

  private ArrayList<String> mIdList;

  public TabGroupActivity() {
  }

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

    startChildActivity("firstChildActivity", new Intent(this, FirstChildActivity.class));
  }

  /**
   * 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();

    } else {
      super.onBackPressed();
    }
  }

}

我建議你應該創建一個tabHost包含所有5個活動A,B,C,D,E。

初始化tabHost后,讓我們通過調用以下方法隱藏此標簽欄:

tabHost.getTabWidget.setVisibility(View.GONE);

你應該像這樣在每個Activity之間切換:

public boolean onOptionsItemSelected( MenuItem item ) 
{
switch (item.getItemId()) 
{
    case R.id.menu_item_A:
        tabHost.setCurrentTab(0);
        return true;

    case R.id.menu_item_B:
        tabHost.setCurrentTab(1);
        return true;

    case R.id.menu_item_C:
        tabHost.setCurrentTab(2);
        return true;

    case R.id.menu_item_D:
        tabHost.setCurrentTab(3);
        return true;

    case R.id.menu_item_E:
        tabHost.setCurrentTab(4);
        return true;

    return false;
}

所以,當你在A1 - > A2,並切換到活動B,並使用菜單切換回A,你仍然在A2。

希望它可以幫到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM