我有一个活动,它是整个应用程序中使用的主要活动,它有许多变量。我有另外两个活动,我希望能够使用来自第一个活动的数据。
现在我知道我可以这样做:
GlobalState gs = (GlobalState) getApplication();
String s = gs.getTestMe();
然而,我想要分享很多变量,其中一些可能相当大,所以我不想像上面那样创建它们的副本。
是否有一种方法可以直接获取和更改变量而不使用get和set方法?我记得在谷歌开发网站上读过一篇文章,说不建议在Android上使用这种性能。
如果你的意图是从当前Activity调用其他Activity,你应该使用intent。您可以将重点放在按需共享数据而不是持久化数据上。
However, if you really need to persist these values then you could persist them in some kind of structured text file or database on local storage. A properties file, XML file, or JSON file could store your data and be easily parsed during activity creation. Don't forget also that you have SQLite on all Android devices, so you could store them in a database table. You could also use a Map to store key-value pairs and serialize the map to local storage, but this might be too cumbersome to be useful for simple data structures.