2

在我的应用程序中,我在底部有四个选项卡。每个选项卡都有多个活动。为了在单个选项卡中维护多个活动,我使用了 TabGroupActivity 的概念。我的问题是,当我单击特定选项卡时,它的起始活动有在该选项卡的某些子活动处于 onResume() 状态时重新启动。为了清楚地理解,

  Tab1-A-->B-->C-->D-->E.
  Tab2-M-->N-->B-->C-->D-->E.
  Tab3-P.
  Tab4-X-->Y-->B-->C-->D-->E.

当我按下 Tab1 时,必须重新启动 A 活动,而 B 或 C 或 D 或 E 活动之一处于 onresume() 状态。同样,Tab2、Tab4 也必须完成。请有人提供我有示例代码,因为我是一个新人。在此先感谢。

4

2 回答 2

1

我找到了我的问题的解决方案,我只是覆盖了 TabGorupActivity 中的 onResume() 方法。我的 TabGroupActivity 是

 package com.rmn.hrt;

    import java.util.ArrayList;



    import android.app.Activity;
    import android.app.ActivityGroup;
    import android.app.AlertDialog;
    import android.app.LocalActivityManager;
    import android.content.DialogInterface;
    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();
          }
    }
 //Removing all string identifiers for the child activities
// except the first activity from mIdList and showing it's view
    @Override
    protected void onResume() {
         LocalActivityManager startManager=getLocalActivityManager();
            int index = mIdList.size() - 1;
            while(index>=1){
            startManager.destroyActivity(mIdList.get(index), true);
            mIdList.remove(index);
            index--;
            }
            String lastId = mIdList.get(index);
            Intent lastIntent = startManager.getActivity(lastId).getIntent();
            Window newWindow =startManager.startActivity(lastId, lastIntent);
            setContentView(newWindow.getDecorView());
        super.onResume();
    }

    }
于 2012-07-05T10:28:58.047 回答
0

试试这个

TabWidget twidget = mTabHost.getTabWidget();

   twidget.getChildAt(0).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    //Start you base activity
     mTabHost.setCurrentTab(0);
     }
   });
于 2012-07-02T13:06:53.587 回答