当我在Android中按下“后退"按钮时,如何在活动之间传递数据? [英] How do I pass data between activities when I press back button in Android?

查看:151
本文介绍了当我在Android中按下“后退"按钮时,如何在活动之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法在活动之间发送数据:

I'm trying to send data between activities using:

关于活动2

Bundle bloc = new Bundle();
bloc.putString("DataLoc", et1.getText().toString());
Intent intent = new Intent(this, Activity2.class);

intent.putExtras(bloc);

关于活动1

Intent iinf = getIntent();
Bundle binf = iinf.getExtras();

if (binf != null) {
  String data = binf.getString("DataInf");
  tv_1.setText(data);
  tv_1.setText(getIntent().getExtras().getString("DataInf"));

}// end if

我的问题

我在Activity2上(打开了Activity1 under Activity2),当我按下后退"按钮时,我需要在一个TextView,EditText或类似文本上显示捆绑包,但只有我可以显示onCreate方法.

I'm on Activity2 (with Activity1 under Activity2 opened), when I press back button I need show bundle on one TextView, EditText or similar but only I get to show onCreate method.

我尝试使用onResumeonRestart,但是...不可能.

I try using onResume and onRestart but... impossible.

有什么建议吗?

推荐答案

当我按下返回按钮时,如何在活动之间传递数据 Android吗?

How do I pass data between activities when I press back button in Android?

使用 StartActivityForResult ,并使用父活动中的onActivityResult来使用从第二个活动接收到的TextView进行更新

Start second Activity using StartActivityForResult and use onActivityResult in parent Activity for updating TextView using received from second Activity

在第二个活动"中,重写onBackPressed()方法并使用Intent调用setResult发送数据:

In Second Activity override onBackPressed() method and call setResult for send data using Intent:

    @Override
    public void onBackPressed() {
        Intent data = new Intent();
        // add data to Intent 
        setResult(Activity.RESULT_OK, data);
       super.onBackPressed();
}

这篇关于当我在Android中按下“后退"按钮时,如何在活动之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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