'I made a user database of Online-users in Firebase database using in activity first but when i use intent to go to other activity , it vanishes

I want to maintain online-user firebase db throughout the app, not just an activity.

in onCreate() i'm building the fire db and in onStop() I'm removing the user from online table.

i just want to destroy user presence only if it leaves the app and maintain presence all over the app activities.

here is my code

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_rps_landing_page);
  databaseReference.child("Online-users").child(userId).setValue(
    new User(getIntent().getStringExtra("userName"), image));}
  }

  @Override
  protected void onStop() {
    super.onStop();
    String userId = getIntent().getStringExtra("userId");
    database = FirebaseDatabase.getInstance();
    databaseReference = database.getReference();
    databaseReference.child("Online-users").child(userId).removeValue();
  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source