[英]How to transfer the Firebase reference to another android activity
我們有2個Android活動(活動A和活動B)
假設我們在活動A中實例化了Firebase引用。活動A還處理所有用戶身份驗證(Facebook,Google和電子郵件/密碼)。
活動B(使用Android意圖從活動A啟動)應使用Firebase參考來訪問Firebase服務器上的某些數據。
現在我想知道將Firebase ref從活動A發送到活動B的最佳設計是什么。
使用Java Serializable將Firebase引用與intent捆綁在一起,並在Activity B中使用intent.getExtra()
使用Android Parcelable將Firebase參考與意圖捆綁在一起,並在活動B中使用intent.getExtra()
在Android服務中啟動Firebase引用,並將每個活動綁定到該服務
還有別的。
執行此操作的常用方法是將數據的URL傳遞給新活動。 例如,請參閱Firebase Android Drawing示例中的 此方法 :
private void openBoard(String key) {
Log.i(TAG, "Opening board "+key);
Toast.makeText(BoardListActivity.this, "Opening board: "+key, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, DrawingActivity.class);
intent.putExtra("FIREBASE_URL", FIREBASE_URL);
intent.putExtra("BOARD_ID", key);
startActivity(intent);
}
然后,新活動會讀取網址並構建新的Firebase
參考:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
final String url = intent.getStringExtra("FIREBASE_URL");
final String boardId = intent.getStringExtra("BOARD_ID");
Log.i(TAG, "Adding DrawingView on "+url+" for boardId "+boardId);
mFirebaseRef = new Firebase(url);
在這些調用之間確實保持了身份驗證狀態。 Firebase SDK為應用程序會話維護與服務器的單一連接,並且每個Firebase
引用都是一個輕量級引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.