最佳实践:在Android活动之间传递对象

son*_*nix 7 user-interface android

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

我看到2个基本选项:

  1. 将intent中的content-id作为URI(intent.putExtra())传递,并在活动之间传递它们
  2. 将content-id保存到本地存储,并在登录后再次加载

还有其他选择和最佳做法吗?

AC2*_*C28 2

我建议使用SharedPreferences,它类似于选项 2。它允许您在应用程序关闭后获取 content-id(或字符串或 json 对象)。您还可以在将 content-id 放入共享首选项之前对其进行加密

除了意图(ram)和本地存储(rom/sdcard,包括数据库)之外,我看不到任何其他选项(本地)。

情况 1:您需要在应用程序关闭后恢复活动,
您应该使用本地存储

情况2:应用关闭后无需恢复Activity

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) 
Run Code Online (Sandbox Code Playgroud)