2

私のアプリケーションでは、下部に 4 つのタブがあります。各タブには複数のアクティビティがあります。単一のタブで複数のアクティビティを維持するために、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 を押すと、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 に答える