在BackPressed上将数据发送到父活动 [英] Send data to a parent activity onBackPressed

查看:71
本文介绍了在BackPressed上将数据发送到父活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在按下返回键时将更新的数据发送到父活动?我想更新捆绑软件中的数据,但看不到如何访问它.

Is there a way to send updated data to a parent activity when back is pressed? I'd like to update the data in the bundle, but I don't see how I would access it.

例如,我有一个图库活动可以打开图像查看器.假设用户滚动浏览了十几个图像,然后返回画廊.最好将画廊中的焦点图像更新为他们最近查看的图像.

For example, I have a gallery activity that opens an image viewer. Say a user scrolls through a dozen images and then backs out to the gallery. It would be ideal to update the focal image in the gallery with the last image they viewed.

此刻,如果没有全局设置,我想不起来怎么做.

At the moment I can't think of how to do so without a global setting.

这是我想做的事情的伪代码(尽管这显然行不通):

Here's pseudo code of what I'd like to do (although this obviously wouldn't work):

孩子:

@Override
public void onBackPressed() {
    getIntent().setData(currentImage);  // Not the right intent, obviously
    super.onBackPressed();
}

父母:

@Override
public void onResume()
{
    super.onResume();
    Uri photoFocus = getIntent().getData();
    if (photoFocus != null)
        setPhotoFocus(photoFocus);
}

推荐答案

可以做到

startActivityForResult()

startActivityForResult()

当您开始儿童活动时,从您的父母活动中

开始. onBackPressed,您可以在childActivity中调用setResult().

from your parent activity when you start childactivity. onBackPressed, you can call setResult() in your childActivity.

在parentActivity中,代码将返回回调:

In parentActivity the code will come to callback :

受保护的void onActivityResult(int requestCode,int resultCode, 意图数据){}

protected void onActivityResult(int requestCode, int resultCode, Intent data) { }

您可以在其中提取setResult方法中的结果集

where you can extract the result set in setResult method

OR

使用sharedPreferences

这篇关于在BackPressed上将数据发送到父活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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