我想知道在 Activity 之间传递对象的最佳做法是什么? 我注销的用户在一个内容项上,应该能够在登录过程后返回到该项目。因此我需要在这些 Activity 之间传递内容 ID
我看到 2 个基本选项:
- 将 Intent 中的 content-id 作为 URI (intent.putExtra()) 传递并在 Activity 之间传递
- 保存content-id到本地存储,登陆后重新加载
还有其他选择和最佳做法吗?
最佳答案
我建议使用 SharedPreferences这就像选项 2。 这允许您在应用程序关闭后获取内容 ID(或字符串或 json 对象)。 您还可以在将内容 ID 放入 sharedPreferences 之前对其进行加密
除了 Intent (ram)和本地存储(rom/sdcard,包括数据库),我看不到任何其他选项(本地)。
Case 1: you need to resume activity after the app is closed
you should use local storageCase 2: you don't need to resume activity after the app is closed
option 1: 0. load the first activity 1. start login_activity (startActivityForResult()) (do not call finish() ) 2. after login is done (call finish()) 3. activity is resumed (if login fail -> redirect to other activity ) option 2: 1. create a public class with a data member to save the content-id/activity class (you may assign singleton design pattern)
关于android - 最佳实践 : passing objects between Android activities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25019512/