如何将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).
活动B(使用Android意向从活动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.
现在,我想知道将活动A的Firebase引用发送到活动B的最佳设计是什么.
Now I was wondering what the best design is to send the Firebase ref from activity A to activity B.
-
使用Java Serializable将Firebase引用与意图捆绑在一起,并在活动B中使用intent.getExtra()
Bundle the Firebase reference with the intent using Java Serializable and use intent.getExtra() in Activity B
使用Android Parcelable将Firebase引用与意图捆绑在一起,并在活动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);
}
然后,新活动读取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屋!