如何将文本从一个活动传递到所有活动? [英] How to pass a text from one activity to all activities?

查看:84
本文介绍了如何将文本从一个活动传递到所有活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将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","");

这篇关于如何将文本从一个活动传递到所有活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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