android - 最佳实践 : passing objects between Android activities

标签 android user-interface

我想知道在 Activity 之间传递对象的最佳做法是什么? 我注销的用户在一个内容项上,应该能够在登录过程后返回到该项目。因此我需要在这些 Activity 之间传递内容 ID

我看到 2 个基本选项:

  1. 将 Intent 中的 content-id 作为 URI (intent.putExtra()) 传递并在 Activity 之间传递
  2. 保存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 storage

Case 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/

相关文章:

android - 在Android工具栏中使用Lottie动画

android - 如何对齐屏幕底部的 View ?

ios - 如何使用 swift 在点击 View 时隐藏 UI 元素?

java - 为什么不在 Android Studio 中显示 progressBar?

linux - tmux、vim、emacs 等如何超越 *nix 终端的 UI 限制?

java - 什么在 MIDP 编程中相当于 swing-application 的 JButton?

c++ - Qt 有没有类似的替代品?

android - 获取 Intent 及其附加信息,但不是启动新 Activity 的 Activity 的 Intent

android - 在android中将字符串转换为标题大小写

android - 自定义列表适配器不会显示图片