如何将文本从一个活动传递到所有活动?

如何将文本从一个活动传递到所有活动?

问题描述:

我需要将MainActivity.java中的单个文本解析为应用程序中的所有其他活动.
有可能这样做吗?

I need to parse a single text from my MainActivity.java to all other activities in my app.
Is it possible to do so?

只需将文本作为字符串存储在shared preferences中,然后在其他活动中获取该字符串即可.或者您也可以在所有其他活动中使用broadcast receiver.但是首先所有活动应首先调用receiver,然后MainActivity可以发送文本. 在MainActivity中,

Just store the text as string in shared preferences and then get the string in other activities.. or you can also use broadcast receiver in all other activities. But first all the activities should call the receiver first then the MainActivity can send the text. In MainActivity,

this.getSharedPreferences("MyPrefName", Context.MODE_PRIVATE).edit().putString("parsetext","yourtext").apply();

以及其他活动.

this.getSharedPreferences("MyPrefName", Context.MODE_PRIVATE).getString("parsetext","");