How To Transfer The Firebase Reference To Another Android Activity
We have 2 Android activities (activity A and activity B) Suppose that we instantiated a Firebase reference in activity A. Activity A also handles all user authentication (Facebook,
Solution 1:
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:
privatevoidopenBoard(String key){
Log.i(TAG, "Opening board "+key);
Toast.makeText(BoardListActivity.this, "Opening board: "+key, Toast.LENGTH_LONG).show();
Intent intent = newIntent(this, DrawingActivity.class);
intent.putExtra("FIREBASE_URL", FIREBASE_URL);
intent.putExtra("BOARD_ID", key);
startActivity(intent);
}
The new activity then reads the URL and constructs a new Firebase
reference:
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intentintent= getIntent();
finalStringurl= intent.getStringExtra("FIREBASE_URL");
finalStringboardId= intent.getStringExtra("BOARD_ID");
Log.i(TAG, "Adding DrawingView on "+url+" for boardId "+boardId);
mFirebaseRef = newFirebase(url);
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.
Post a Comment for "How To Transfer The Firebase Reference To Another Android Activity"