我应该如何在活动之间进行交流? [英] How should I communicate between activities?

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

问题描述

我有 3 个按钮.按钮 A、B 和 C.按钮 A 驻留在片段中.它开始意图(活动).在新活动按钮 B 和 C 内.按钮 B 显示NEW",而按钮 C 显示OK".

I have 3 buttons. Button A, B, and C. Button A resides in Fragment. It starts intent (activity). Within the new activity button B and C reside. Button B says "NEW" while button C says "OK".

我想要做的是在单击按钮 B(新")之后,意图应该保持该按钮,直到用户点击按钮 C(确定"),活动应该在此处自行销毁并返回现在的片段一个名为(NEW")的新按钮.

What I want to do is after clicking button B ("NEW") the intent should hold that button until the user hits button C ("OK") where the activity should destroy itself and go back to fragment where there is now a new button called ("NEW").

有哪些简单的方法可以做到这一点?如果我希望应用程序记住新创建的按钮以便在 onDestroy 时不会丢失,我应该用 sqlite 保存它吗?

What are some easy ways to do this? And should I save this with sqlite if I want the app to remember the newly created button so that it's not lost upon onDestroy?

我对 Android 不是很精通,所以希望有人能用外行的话说或举个例子.

I'm not very proficient within Android so hopefully someone can put it in laymans terms or point to an example.

推荐答案

使用 Bundle 请阅读更多相关信息.http://developer.android.com/reference/android/os/Bundle.html

Use Bundle please read sth more about it. http://developer.android.com/reference/android/os/Bundle.html

1) 使用来自 Intent 的 Bundle:

1) Use the Bundle from the Intent:

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);

2) 创建一个新的 Bundle

2) Create a new Bundle

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);

3) 使用 Intent 的 putExtra() 快捷方法

3) Use the putExtra() shortcut method of the Intent

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);

然后,在启动的 Activity 中,您可以通过以下方式读取它们:

Then, in the launched Activity, you would read them via:

String value = getIntent().getExtras().getString(key);

这是正确的方法.另一种方法是 SharedPreferences.http://developer.android.com/training/basics/data-storage/shared-首选项.html

This is the proper way to do that. Another way is SharedPreferences. http://developer.android.com/training/basics/data-storage/shared-preferences.html

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

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