Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

java - 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, Google, and email/password).

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.

Now I was wondering what the best design is to send the Firebase ref from activity A to activity B.

  1. Bundle the Firebase reference with the intent using Java Serializable and use intent.getExtra() in Activity B

  2. Bundle the Firebase reference with the intent using Android Parcelable and use intent.getExtra() in activity B

  3. Initiate the Firebase reference in a Android service and bind each activity to that service

  4. Something else.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

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);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...