当我在写入后写入Firebase数据库时,活动将重新启动并启动mainactivity
这是一个示例活动
public class BloodNewPost extends AppCompatActivity {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String email = user.getEmail();
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private ImageView backbtn;
private ImageView bloodnewpostprofile;
private TextView bloodemail;
private EditText blooddescblooddesc;
MaterialBetterSpinner bloodGroupSpinner;
private MaterialButton bloodsubmitbtn;
private AVLoadingIndicatorView aviLoading;
String[] bloodLIST = {"A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"};
String phone;
String city;
String group;
String desc;
String intentUserID = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blood_new_post);
//Getting data intent from blooddetailActivity
Intent intent = getIntent();
intentUserID = intent.getStringExtra("bloodUID");
//Assign the database ref
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabase.keepSynced(true);
mAuth = FirebaseAuth.getInstance();
backbtn = findViewById(R.id.backbtn);
bloodnewpostprofile = findViewById(R.id.bloodnewpostprofile);
bloodemail = findViewById(R.id.bloodemail);
blooddescblooddesc = findViewById(R.id.blooddescblooddesc);
bloodGroupSpinner = findViewById(R.id.bloodgroupspinner);
bloodsubmitbtn = findViewById(R.id.bloodsubmitbtn);
aviLoading = findViewById(R.id.aviloading);
//Display profile info
displayProfilePicture();
bloodemail.setText(email);
//if not new user then use this for update data else create new data
displayBloodDataifExist();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, bloodLIST);
bloodGroupSpinner.setAdapter(arrayAdapter);
bloodGroupSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
group = parent.getItemAtPosition(position).toString();
}
});
backbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent backToMain = new Intent(BloodNewPost.this,BloodBankHomeActivity.class);
startActivity(backToMain);
finish();
}
});
bloodnewpostprofile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent backToMain = new Intent(BloodNewPost.this,ProfileActivity.class);
startActivity(backToMain);
finish();
}
});
bloodemail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent backToMain = new Intent(BloodNewPost.this,ProfileActivity.class);
startActivity(backToMain);
finish();
}
});
Log.d("Check",phone + city + desc);
bloodsubmitbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//set Data to the firebase databse
setBloodData();
}
});
}
private void displayBloodDataifExist() {
mDatabase.child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("Blood-Bank")){
if (dataSnapshot.child("Blood-Bank").hasChild("group")){
bloodGroupSpinner.setText(dataSnapshot.child("Blood-Bank").child("group").getValue().toString());
blooddescblooddesc.setText(dataSnapshot.child("Blood-Bank").child("desc").getValue().toString());
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void displayProfilePicture() {
mDatabase.child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("profileImage")){
String image = dataSnapshot.child("profileImage").getValue().toString();
if (image.equals("")){
bloodnewpostprofile.setImageResource(R.mipmap.userpics);
}else {
Picasso.get().load(image).placeholder(R.mipmap.userpics).into(bloodnewpostprofile);
}
}else {
bloodnewpostprofile.setImageResource(R.mipmap.userpics);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(BloodNewPost.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
}
private void setBloodData() {
startLoading();
desc = blooddescblooddesc.getText().toString().trim();
if (TextUtils.isEmpty(group)){
bloodGroupSpinner.setError("Please Select Group");
stopLoading();
return;
}else if (TextUtils.isEmpty(desc)) {
blooddescblooddesc.setError("Please write description eg. any disease *");
stopLoading();
return;
}
BloodUtils bloodUtils = new BloodUtils();
bloodUtils.setUserid(mAuth.getCurrentUser().getUid());
bloodUtils.setGroup(group);
bloodUtils.setDesc(desc);
HashMap<String,Object> map = new HashMap<>();
map.put("userid",bloodUtils.getUserid());
map.put("group",bloodUtils.getGroup());
map.put("desc",bloodUtils.getDesc());
mDatabase.child(mAuth.getCurrentUser().getUid()).child("Blood-Bank").updateChildren(map, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(@Nullable DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
if (databaseError != null){
Toast.makeText(BloodNewPost.this, "Something went wrong "+databaseError, Toast.LENGTH_SHORT).show();
}else {
Intent intent = new Intent(BloodNewPost.this,BloodBankHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
stopLoading();
}
});
}
void startLoading(){
aviLoading.smoothToShow();
aviLoading.setVisibility(View.VISIBLE);
bloodsubmitbtn.setVisibility(View.INVISIBLE);
}
void stopLoading(){
aviLoading.smoothToHide();
aviLoading.setVisibility(View.INVISIBLE);
bloodsubmitbtn.setVisibility(View.VISIBLE);
}
}
这是运行结果
*
E / RecyclerView:未连接适配器;跳过布局V / FA:屏幕 暴露时间少于1000毫秒。活动未发送。时间:328 V / FA: onActivityCreated W /资源:转换为字符串: TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a002e} 转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a003b} W /资源:转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a002c} W /资源:转换为字符串: TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a0081} V / FA:活动已暂停,时间: 19185174 W /资源:转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a0142} W /资源:转换为字符串: TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a0188} W /资源:转换为 字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a014d} W /资源: 转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a0073} W /资源:转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00f9} 转换为字符串:TypedValue {t = 0x1d / d = 0xffffffff a = 2 r = 0x7f060055} W /资源:转换为字符串: TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00cd} 转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a0160} W /资源:转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00de} W /资源:转换为字符串: TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00cd} W /资源:转换为 字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a0160} W /资源: 转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00de} W /资源:转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00cd} W /资源:转换为字符串: TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a0160} W /资源:转换为 字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00de} W /资源: 转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00cd} 转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a0160} W /资源:转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00de} W /资源:转换为字符串: TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00cd} W /资源:转换为 字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a0160} W /资源: 转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0a00de} D / FA: 记录事件(FE):screen_view(_vs), Bundle [{firebase_event_origin(_o)= auto, firebase_previous_class(_pc)= MainActivity, firebase_previous_id(_pi)=-8673039228870038719, firebase_screen_class(_sc)= MainActivity, firebase_screen_id(_si)=-8673039228870038718}] E / RecyclerView:否 连接适配器;跳过版式V / FA:活动已恢复,时间: 19185264 E / RecyclerView:未连接适配器;跳过版式V / FA: 处于不活动状态,与服务V / FA断开连接:正在记录用户 参与度,毫秒:489152 V / FA:连接到远程服务V / FA: 活动已暂停,时间:19674413 D / FA:记录事件(FE): user_engagement(_e),捆绑包[{firebase_event_origin(_o)=自动, engagement_time_msec(_et)= 489152, firebase_screen_class(_sc)= MainActivity, firebase_screen_id(_si)=-8673039228870038718}] V / FA:连接 D / FA:已连接到远程服务V / FA: 处理排队的服务任务:2 V / FA:不活动,正在断开连接 从服务中
*