The activity restart after write to firebase realtime database android

0

when i write to firebase database after write the activity will restart and start the mainactivity

this is a activity for one example

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

}

this is run results

*

E/RecyclerView: No adapter attached; skipping layout V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 328 V/FA: onActivityCreated W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a002e} Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a003b} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a002c} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a0081} V/FA: Activity paused, time: 19185174 W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a0142} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a0188} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a014d} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a0073} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00f9} Converting to string: TypedValue{t=0x1d/d=0xffffffff a=2 r=0x7f060055} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00cd} Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a0160} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00de} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00cd} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a0160} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00de} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00cd} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a0160} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00de} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00cd} Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a0160} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00de} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00cd} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a0160} W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0a00de} D/FA: Logging event (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: No adapter attached; skipping layout V/FA: Activity resumed, time: 19185264 E/RecyclerView: No adapter attached; skipping layout V/FA: Inactivity, disconnecting from the service V/FA: Recording user engagement, ms: 489152 V/FA: Connecting to remote service V/FA: Activity paused, time: 19674413 D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=489152, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8673039228870038718}] V/FA: Connection attempt already in progress D/FA: Connected to remote service V/FA: Processing queued up service tasks: 2 V/FA: Inactivity, disconnecting from the service

*


android
firebase
firebase-realtime-database
firebase-authentication
asked on Stack Overflow May 24, 2019 by Prem Singh Sodha • edited May 24, 2019 by Zoe

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0