0

我正在开发一个应用程序,其中包含TabHost在其中一个选项卡中我有一个ActivityGroup,然后ActivityGroup我启动另一个SubActivity(假设我启动一个ActivityA),直到这个,一切正常。

问题是当我按下BackButton时,CurrentActivity( ActivityA) 被破坏,但 ParentActivity(The ActivityGroup) 没有恢复,并且应用程序只显示一个带有我的应用程序标题的空窗口(“我的应用程序标题”)。

Activity从我启动 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

我想要的是显示ActivityGroupActivity的 A 被摧毁的时间。

注意我的应用程序级别是 4: * Android 1.6 *,所以我无法 override 使用该方法 onBackPressed()

感谢大家的帮助

- - - - - - - - - - - - - - - - - -编辑 - - - - - - - --------------------------

onKeyDown在我Activity的 A 上添加了这样的代码:

@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);
    }

我得到了同样的结果,ActivityA被停止了,但它仍然给我一个带有我的应用程序标题的空窗口,并且它不显示我的ActivityGroupParentActivity

4

1 回答 1

1

当我第一次开始尝试ActivityGroups 时,我遇到了类似的问题。问题是您需要将您onKeyDown()Activity. 但是,您需要Activity引用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();
        }
    }

}

接下来是Activity的相关代码:

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 个)。本质上,只需将 Group 设为 Singleton,这样您就可以始终拥有相同的实例,并保留 View 数组,以便拥有 History。然后在单击返回或添加视图时引用历史记录。

希望这会有所帮助, FuzzicalLogic

于 2012-01-05T12:20:39.880 回答