Firebase Database reopens activity when a value changes

I have a problem in my activity when I change a value in the database. I tried creating a new Activity with a toolbar, the toolbar's title is changed with a database value via ValueEventListener. The problem is that when I change the value in my DB, the activity reopens itself.

private static String eventKey;

private FirebaseAuth mAuth;
private DatabaseReference mDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testing);

    mAuth = FirebaseAuth.getInstance();
    if (mAuth.getCurrentUser() == null) {
        finishAffinity();
        startActivity(new Intent(this, HomeScreenActivity.class));
    }

    eventKey = getIntent().getExtras().getString("eventKey");

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Eventos").child(eventKey);
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                String titleValue = dataSnapshot.child("title").getValue().toString();
                getSupportActionBar().setTitle(titleValue);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

This is the full code of the TestingActivity class.

Here you can see the previous activity (is too large). And here you can see the log when I change something in my DB.

Thanks!

2 answers

  • answered 2018-07-11 04:14 Jonas Lochmann

    if (mAuth.getCurrentUser() == null) {
        finishAffinity();
        startActivity(new Intent(this, HomeScreenActivity.class));
    }
    

    This tends to cause issues. You should wait for an value before checking it.

  • answered 2018-07-11 04:38 dr3k

    By looking your code provided in the link, it seems you are launching the TestingActivity or EventSingleActivity.

    This is the code from the link:

                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
    
                        mDatabase.child(id).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String eventTime = dataSnapshot.child("timeStamp").getValue().toString();
                                if(Integer.parseInt(nowTime) > Integer.parseInt(eventTime)){
                                    Intent intent = new Intent(PrincipalActivity.this, TestingActivity.class);
                                    intent.putExtra("eventKey", id);
                                    startActivity(intent);
                                    finish();
                                } else {
                                    Intent intent = new Intent(PrincipalActivity.this, EventSingleActivity.class);
                                    intent.putExtra("eventKey", id);
                                    startActivity(intent);
                                }
                            }
    
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
    
                            }
                        });
                    }
                });
    

    That's why every changes from the firebase db it will launch the activities I mentioned above.

    To fix this you need to do the following:

                mDatabase.child(id).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                      String eventTime = dataSnapshot.child("timeStamp").getValue().toString();
                                                    if(Integer.parseInt(nowTime) > Integer.parseInt(eventTime)){
                                                        Intent intent = new Intent(PrincipalActivity.this, TestingActivity.class);
                                                        intent.putExtra("eventKey", id);
                                                        startActivity(intent);
                                                        finish();
                                                    } else {
                                                        Intent intent = new Intent(PrincipalActivity.this, EventSingleActivity.class);
                                                        intent.putExtra("eventKey", id);
                                                        startActivity(intent);
                                                    }
    
                                    }
                                });
                            }
    
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
    
                            }
                        });