0

TabHost私が持っているタブの1つに、を含むアプリを開発しています。このタブActivityGroupから、ActivityGroup別のタブSubActivityを起動します(Aを起動するとしましょうActivity)。これまで、すべて問題ありません。

問題は、BackButton を押す、CurrentActivity( ActivityA) が破棄されますが、ParentActivity(The ActivityGroup) は再開されず、アプリは My App のタイトル (「My Application Title」) を持つ null ウィンドウのみを表示します。

Activitymy から Aを起動するコードActivityGroupは次のとおりです。

View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
this.setContentView(view);

そして、私は私の中にこのようなoverrided方法を持っています:onKeyDownActivityGroup

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i(TAG, "onKeyDown");
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
            Activity current = getLocalActivityManager().getCurrentActivity();
            Log.i(TAG, current.getIntent().getStringExtra("id"));
            current.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

onKeyDownしかし、aiがログ「onKeyDown」を表示しなかったため、メソッドが呼び出されないようです。

そしてlogcatはこれだけを表示します:

01-05 11:04:38.012: W/KeyCharacterMap(401): No keyboard for id 0
01-05 11:04:38.012: W/KeyCharacterMap(401): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

私が欲しいのは、私のAが破壊されたActivityGroupときに表示することです。Activity

NB私のアプリレベルは4です: * Android 1.6 *なので override 、メソッドはできません onBackPressed()

ご協力ありがとうございました

- - - - - - - - - - - - - - - - - -編集 - - - - - - - --------------------------

onKeyDown私は私の A にこのようなコードを追加しましActivityた:

@Override public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
        ParentActivity parentActivity = (ParentActivity) this.getParent();
        parentActivity.onKeyDown(keyCode, event);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

そして私の中にParentActivity、私は持っています:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
            Log.i(TAG, "onKeyDown");
            int len = idOfSubActivities.size();
            String idOfCurrentActivity = idOfSubActivities.get(len-1);
            Activity currentActivity = getLocalActivityManager().getActivity(idOfCurrentActivity);
            currentActivity.finish();
            idOfSubActivities.remove(len - 1);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

同じ結果が得られ、 A は停止しましたが、アプリのタイトルが表示された null ウィンドウが表示され、 ( )Activityが表示されません。ActivityGroupParentActivity

4

1 に答える 1

1

私が最初にActivityGroups の実験を始めたとき、私はこれに似た問題に直面しました。onKeyDown()問題は、 を に配置する必要があることですActivityActivityただし、への参照が必要ですActivityGroup。次に、押し戻すときに、 で自分自身を呼び出すだけonBack()ですActivityGroup

(編集)ここにあなたのためのサンプルがあります

以下は、私のアプリでナビゲーションと履歴を処理する ActivityGroup コードを削除したものです。勝手に調整してますので、誤差があるかもしれません。いくつかの細かい点に注意してください。

public class MyGroup extends ActivityGroup
{
/** Static Reference to this Group. */
    static MyGroup instance;
/** Keeps Track of the History as a Stack. */
    private ArrayList<View> myActivityHistory;

    @Override protected void onCreate(final Bundle savedInstanceState)
    {//Call the Base Implementation
        super.onCreate(savedInstanceState);

    // Initialize the Activity History
        myActivityHistory = new ArrayList<View>();

    // Build the Intent
        Intent _root = null;
    //Lists the Applications
        _root = new Intent(this, MyActivity.class);
    // Send the Index to the Child Activity
        _root.setAction(Intent.ACTION_VIEW);
    // Forward the Extras, if they are there
    // Start the root Activity within the Group and get its View
        final View _view = getLocalActivityManager().startActivity("App Preferences", _root).getDecorView();
    // Start the History
        addNewLevel(_view);
    }

    /**
     * Gets the instance of the {@link ApplicationGroup} that the child Activity
     * belongs to.
     *
     * @param index
     *  The Group that was passed to the child in the {@link android.content.Intent
     *  Intent} via an Extra (int).
     * @return
     *  <b>ApplicationGroup -</b> The group that this child was assigned to.
     */
    static public ApplicationGroup getGroup()
    {   if (instance != null)
            return instance;
    }

    /**
     * Allows the Child to replace the {@link ApplicationGroup}'s current
     * {@link android.view.View View} with the specified View. This is
     * intended to be used specifically by the Child.
     *
     * @param withView
     *  The View to use for replacement.
     */
    public void addNewLevel(final View withView)
    {//Adds the old one to history
        myActivityHistory.add(withView);
    // Changes this Groups View to the new View.
        setContentView(withView);
    }

    /**
     * Takes the specified {@link android.app.ActivityGroup ActivityGroup} back
     * one step in the History to the previous {@link android.view.View View}.
     */
    public void back()
    {   Log.d("Group", "Back overridden");
        //If there are more than one screen
        if (myActivityHistory.size() > 1)
        {   Log.d("Group", "History called");
        // Remove the most recent View
            myActivityHistory.remove(myActivityHistory.size()-1);
        // Change the View back.
            setContentView(myActivityHistory.get(myActivityHistory.size()-1));
        }
    // Otherwise Exit
        else
        {   Log.d("Group", "Program finished");
            finish();
        }
    }

}

次は、アクティビティに関連するコードです。

public boolean onKeyDown(int keyCode, KeyEvent event)
{//If back was pressed
    if (keyCode==KeyEvent.KEYCODE_BACK)
    {   MyGroup.getGroup().back();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

KeyDownListener を面白いものに設定していないことを確認してください。正常に動作するはずです。:) 私が行った変更は、実際にはグループの配列 (一度に 3 つ) にあるためです。基本的に、グループをシングルトンにして、常に同じインスタンスを保持できるようにし、ビューの配列を保持して履歴を保持できるようにします。次に、[戻る] をクリックするか、ビューを追加するときに履歴を参照します。

これが役に立てば幸いです、FuzzicalLogic

于 2012-01-05T12:20:39.880 に答える