活动之间进行导航在的ActivityGroup [英] Navigate Between Activities in an ActivityGroup

查看:225
本文介绍了活动之间进行导航在的ActivityGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发包含一个应用程序 TabHost ,在这些选项卡之一,我有一个的ActivityGroup ,和从这个的ActivityGroup ,我推出另一子活动(比方说,我启动了活动 A),直到这一点,一切都OK了。

I'm developing an app which contain TabHost ,in one of those tabs i have an ActivityGroup , and from this ActivityGroup, i launch another SubActivity ( let's say that i Launch an Activity A ), and until this , everything is OK.

问题是,当我preSS在后退按钮活动 A)被破坏的CurrentActivity,但ParentActivity(即的ActivityGroup )不恢复,以及应用程序展示只是一个空窗与我的应用程序的标题(我的应用程序标题)。

The problem is when i press the BackButton ,the CurrentActivity( Activity A ) is destroyed, but the ParentActivity( The ActivityGroup ) is not resumed , and the app show just a null Window with the Title of My App ( "My Application Title" ) .

在code从启动活动 A我的的ActivityGroup 是:

The Code to launch the Activity A from my ActivityGroup is :

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

和我有 overrided 方法的onkeydown 像这样在我的的ActivityGroup

and i have overrided the method onKeyDown like this in my 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 永远不会被调用,因为我没有拿到日志的onkeydown显示。

but it seems that the method onKeyDown is never called because a i didn't get the Log "onKeyDown" displayed.

和logcat的显示眼前这个:

and the logcat display just this :

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被销毁。

NB 我的应用水平是4: *的 Android 1.6的的*,让我无法 覆盖 onBack pressed()

NB : my app level is 4 : *Android 1.6* , so i can't override the method onBackPressed()

感谢大家的帮助。

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

我已经加入我的的onkeydown 这样对我的活动 A的code:

I have added the code of my onKeyDown like this on my Activity A :

@覆盖
    公共布尔的onkeydown(INT键code,KeyEvent的事件){

@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被停止,但它仍然给我和我的应用程序的标题空窗,它不显示我的的ActivityGroup ParentActivity

I got the same result , the Activity A is stopped , but it still give me the null window with the title of my app , and it doesn't display my ActivityGroup (ParentActivity)

推荐答案

我遇到类似这样的问题时,我第一次开始与的ActivityGroup 取值试验。问题是,你需要把你的的onkeydown()活动。但是,您需要在活动具有对的ActivityGroup 的参考。然后,当你preSS回来,只是叫自己的 onBack()的ActivityGroup

I faced a problem similar to this when I first started experimenting with ActivityGroups. The issue is that you need to place your onKeyDown() in your Activity. However, you need the Activity to have a reference to the ActivityGroup. Then, when you press back, just call your own onBack() in the ActivityGroup.

(编辑)下面是一个示例为您

下面是一个剥离下来的ActivityGroup code处理导航和历史在我的应用程序。它已被调节上飞,所以有可能是一个错误。注意几个细节问题。

Below is the stripped down ActivityGroup code that handles navigation and history in my apps. It has been adjusted on the fly, so there might be an error. Notice a couple of finer points.

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

}

接下来是中肯code的活动:

Next is the pertinent code for the 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)组的阵列。从本质上讲,只是使集团一个Singleton,所以你总是可以有相同的实例,并保持你的意见的数组,让您有一个历史。当您单击后退或者当您添加视图,则引用的历史。

Just make sure that you aren't setting the KeyDownListener to anything funny and it should work fine. :) The changes that I made are because I actually have them in an array of Groups (3 at one time). Essentially, just make the Group a Singleton so you can always have the same instance, and keep an array of your Views so that you have a History. Then reference the History when you click Back or when you add a View.

希望这有助于
FuzzicalLogic

Hope this helps, FuzzicalLogic

这篇关于活动之间进行导航在的ActivityGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆