在ActivityGroup中的活动之间导航

时间:2012-01-05 11:19:07

标签: android android-tabhost activitygroup onkeydown android-1.6-donut

我正在开发一个包含TabHost的应用,在其中一个标签中我有一个ActivityGroup,从这个ActivityGroup开始我推出另一个SubActivity(让我们说我发起Activity A),直到这一切,一切正常。

问题是当我按 BackButton 时,CurrentActivity(Activity A)被销毁,但是ParentActivity(ActivityGroup)没有恢复,而app只显示一个带有我的应用程序标题的空窗口(“我的应用程序标题”)。

Activity启动ActivityGroup A的代码是:

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

我的overrided中有onKeyDown这样的方法ActivityGroup

@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因为我没有显示Log“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

我想要的是在ActivityGroup A被摧毁时显示Activity

NB 我的应用级别为4: * Android 1.6 *,所以我不能 { {1}} 方法 override

谢谢大家的帮助

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

我在onBackPressed() A上添加了onKeyDown这样的代码:

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

Activity

在我的 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已停止,但它仍然给我带有我的应用程序标题的空窗口,并且它不会显示我的Activity({{1 }})

1 个答案:

答案 0 :(得分:1)

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

}

接下来是活动的相关代码:

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设置为任何有趣的东西,它应该可以正常工作。 :)我所做的更改是因为我实际上将它们放在一个Group数组中(一次3个)。基本上,只需使组成为单例,这样您就可以始终拥有相同的实例,并保留一组视图,以便拥有历史记录。然后在单击“上一步”或添加“视图”时引用“历史记录”。

希望这有帮助, FuzzicalLogic