使用.putExtra()或其他方法将DatabaseReference一个活动传递给另一个活动?

时间:2018-01-22 14:48:46

标签: android firebase android-intent spinner

public class MoreQP extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    FirebaseDatabase database;
    DatabaseReference mDatabase;



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_more_qp);


    filterSearchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showResult();
        }
    });

    resultCard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent showresulPage = new Intent(MoreQP.this, ShowQuestion.class);
            showresulPage.putExtra(mDatabase);
            showresulPage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            MoreQP.this.startActivity(showresulPage);
            finish();
        }
    });

}

public void showResult(){
    resultDisplay.setText(selectedSubject+"\n"+sy+" "+ss+" "+se);
    resultCard.setVisibility(resultCard.VISIBLE);

    //"selectedSubject","sy","ss" etc.. I have taken these values from multiple spinner. 
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Department").child("CSE").child("Year").child(sy).child(ss).child(se).child(whichSemester).child(selectedSubject);


}

}

这是我的代码的快捷版本。我想将Firebase引用“mDatabase”传递给我的第二个Activity。我从多个微调器中获取了所有这些值。那么有没有办法将此引用传递给新活动?我如何在第二次活动中收到它?

2 个答案:

答案 0 :(得分:0)

无需将DatabaseReference对象添加到Intent以便将其用于其他活动,只需创建相同的DatabaseReference并在第二个活动中使用它即可。就是这样!

答案 1 :(得分:0)

您不能将存储/数据库引用放在putExtra中,但您当然可以输入路径的字符串值

'String storageRef = "group/"+UID+"/"+"profileImage";
 String postUploadRef="User/"+UID;'

Intent mServiceIntent = new Intent(ProfileActivity.this, UploadService.class);
        mServiceIntent.putExtra("stream", filePath);
        mServiceIntent.putExtra("stroageRef", storageRef);
        mServiceIntent.putExtra("postUploadRef", postUploadRef);
        startService(mServiceIntent);