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

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

问题描述

我有3个按钮. 按钮A,B和C.按钮A位于Fragment中.它开始意图(活动).在新活动按钮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.

推荐答案

使用捆绑包,请进一步阅读. 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)从意图中使用捆绑包:

1) Use the Bundle from the Intent:

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

2)创建一个新的捆绑包

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);

然后,在启动的活动"中,您将通过以下方式阅读它们:

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- preferences.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天全站免登陆