How to transfer the Firebase reference to another android activity

十年热恋 提交于 2019-11-29 14:48:03

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!