簡體   English   中英

最佳實踐:在Android活動之間傳遞對象

[英]Best practice: passing objects between Android activities

我想知道在活動之間傳遞對象的最佳做法是什么? 我注銷的用戶在內容項上,應該能夠在登錄過程后返回此項。 因此,我需要在這些活動之間傳遞內容ID

我看到2個基本選項:

  1. 將intent中的content-id作為URI(intent.putExtra())傳遞,並在活動之間傳遞它們
  2. 將content-id保存到本地存儲,並在登錄后再次加載

還有其他選擇和最佳做法嗎?

我建議使用SharedPreferences ,它類似於選項2.它允許您在關閉應用程序后獲取content-id(或字符串或json對象)。 您還可以在將content-id放入sharedPreferences之前對其進行加密

除了intent(ram)和本地存儲(rom / sdcard,包括數據庫),我也看不到任何其他選項(本地)。

案例1:您需要在應用關閉后恢復活動
你應該使用本地存儲

案例2:應用關閉后您無需恢復活動

 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) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM