简体   繁体   English

将数据传递回Android Studio中的上一个活动

[英]Passing data back into a previous activity in Android Studio

We have been learning how to open new Activities, in class, but so far the second activity runs its logic from the OnCreate() class. 我们已经在课堂上学习了如何打开新的Activity,但是到目前为止,第二个Activity从OnCreate()类运行其逻辑。

I am wanting to return directly to activity one, from activity two, but run a function in its java when I do so. 我想直接从活动二返回活动一,但是这样做时要在其Java中运行一个函数。

All I've been able to find on the matter is how to effectively write my own "back" button. 关于此事,我所能找到的就是如何有效地编写自己的“后退”按钮。

I am needing to pass a string, as well as an int, that was originally sent from activity one to activity two. 我需要传递一个字符串和一个int,它最初是从活动1发送到活动2的。 I need to compare the received int with the sent int to confirm it is coming from the same source. 我需要将接收到的int与发送的int进行比较,以确认它来自同一来源。

My biggest problem is that we were taught to use OnCreate() . 我最大的问题是,我们被教导要使用OnCreate() This will not work on returning to an Activity, as OnCreate only runs when the instance is first created. 这将无法返回到Activity,因为OnCreate仅在首次创建实例时运行。 If I am creating the instance as I move back, my other function will not work. 如果在回退时创建实例,则其他功能将无法使用。

You can just use an startActivityForResult method. 您可以只使用startActivityForResult方法。

Here is a topic of that: How to manage `startActivityForResult` on Android? 这是一个主题: 如何在Android上管理`startActivityForResult`?

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM