如何在活动之间进行沟通?

Car*_*rl 8 android communication android-activity

我有3个按钮.按钮A,B和C.按钮A位于Fragment中.它开始意图(活动).在新活动按钮B和C驻留.按钮B表示"新",按钮C表示"确定".

我想要做的是点击按钮B("新")之后意图应该按住该按钮,直到用户点击按钮C("OK"),其中活动应该自行销毁并返回到现在有一​​个新按钮的片段叫("新").

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

我不是很熟悉Android,所以希望有人可以用外行术语来表达或者说一个例子.

RMa*_*nik 13

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

1)使用Intent中的Bundle:

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
Run Code Online (Sandbox Code Playgroud)

2)创建一个新的Bundle

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);
Run Code Online (Sandbox Code Playgroud)

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

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
Run Code Online (Sandbox Code Playgroud)

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

String value = getIntent().getExtras().getString(key);
Run Code Online (Sandbox Code Playgroud)

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