Android的活动结果 [英] Android Activity Results

查看:79
本文介绍了Android的活动结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我需要通过它们之间的一个对象数组两项活动。从主要活动,我开始与 startActivityForResult()次要活动,并且传递数组作为以捆绑一个ParcelableArray。这工作得很好,以获得阵列次要活动;我可以拉出来阵列的捆绑,并从那里操纵它。

我遇到的问题是,当我想给数组传递回调用活动。我需要它当用户presses设备上的背面/返回按钮时发生。我试过把数组回一个包,调用的setResult()从内部的onPause()在次级活动,但我猜想这是执行的任务不正确的位置。我已经达到这一结论为 getCallingActivity()的onPause()返回null。

什么是一个捆绑在这种情况下返回到调用活动的正确方法?

主要活动:

 叠B =新包();
    b.putParcelableArray(com.whatever.data项目);    意向文件夹视图=新意图(MainView.this,FolderView.class);
    folderView.putExtras(二);
    startActivityForResult(文件夹视图,1);

次要活动:

 保护无效的onPause(){
    super.onPause();    意向书结果=新的Intent();    束B =新包();
    b.putParcelableArray(com.whatever.data项目);    result.putExtras(二);    的setResult(Activity.RESULT_OK,结果);
}


解决方案

IIRC,我已经迷上的onkeydown()并观看了的KeyEvent。 BUTTON_BACK 键,名为的setResult()那里。在Android 2.0的,你勾 onBack pressed()代替。

I have two activities in which I need to pass an object array between them. From the main activity, I start the secondary activity with startActivityForResult(), and pass the array as a ParcelableArray in a Bundle. This works fine to get the array to the secondary activity; I can pull the array out of the Bundle and manipulate it from there.

The problem I'm having is when I want to pass the array back to the calling activity. I need it to occur when a user presses the back/return button on the device. I've tried placing the array back into a Bundle, calling setResult() from within onPause() on the secondary activity, but I'm assuming this is the incorrect location to perform the task. I've reached that conclusion as getCallingActivity() within onPause() returns null.

What's the proper way to return a Bundle to the calling activity in this situation?

Main Activity:

    Bundle b = new Bundle();
    b.putParcelableArray("com.whatever.data", items);

    Intent folderView = new Intent(MainView.this, FolderView.class);
    folderView.putExtras(b);
    startActivityForResult(folderView, 1);

Secondary Activity:

protected void onPause () {
    super.onPause();

    Intent result = new Intent();

    Bundle b = new Bundle();
    b.putParcelableArray("com.whatever.data", items);

    result.putExtras(b);

    setResult(Activity.RESULT_OK, result);
}

解决方案

IIRC, I've hooked onKeyDown() and watched for KeyEvent.BUTTON_BACK and called setResult() there. In Android 2.0, you'd hook onBackPressed() instead.

这篇关于Android的活动结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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