如何将 Firebase 引用转移到另一个 android 活动 [英] How to transfer the Firebase reference to another android activity
问题描述
我们有 2 个 Android 活动(活动 A 和活动 B)
We have 2 Android activities (activity A and activity B)
假设我们在活动 A 中实例化了一个 Firebase 引用.活动 A 还处理所有用户身份验证(Facebook、Google 和电子邮件/密码).
Suppose that we instantiated a Firebase reference in activity A. Activity A also handles all user authentication (Facebook, Google, and email/password).
Activity B 使用 Android Intent 从 Activity A 启动,应该使用 Firebase 引用来访问 Firebase 服务器上的一些数据.
Activity B, which is started from activity A using an Android intent, should use the Firebase reference to access some data on the Firebase servers.
现在我想知道将 Firebase 引用从活动 A 发送到活动 B 的最佳设计是什么.
Now I was wondering what the best design is to send the Firebase ref from activity A to activity B.
使用 Java Serializable 将 Firebase 引用与 Intent 捆绑在一起,并在 Activity B 中使用 intent.getExtra()
Bundle the Firebase reference with the intent using Java Serializable and use intent.getExtra() in Activity B
使用 Android Parcelable 将 Firebase 引用与 Intent 捆绑在一起,并在活动 B 中使用 Intent.getExtra()
Bundle the Firebase reference with the intent using Android Parcelable and use intent.getExtra() in activity B
在 Android 服务中启动 Firebase 引用并将每个活动绑定到该服务
Initiate the Firebase reference in a Android service and bind each activity to that service
别的东西.
推荐答案
执行此操作的常用方法是将数据的 URL 传递给新活动.参见例如 此方法来自Firebase Android 绘图示例:
The common way to do this is to pass the URL for the data to the new activity. See for example this method from the Firebase Android Drawing sample:
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);
}
新的 Activity 然后读取 URL 并构建一个新的 Firebase
引用:
The new activity then reads the URL and constructs a new Firebase
reference:
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
引用都是最重要的轻量级引用.
The authentication state is indeed maintained between these calls. The Firebase SDK maintains a single connection to the server for an application session and each Firebase
reference is a lightweight reference on top of that.
这篇关于如何将 Firebase 引用转移到另一个 android 活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!