0

I'm currently developing an android app and using firebase realtime database. How to pass the user data from the login activity to the navigation header of home activity?

What should I add inside the Login Activity in order to pass the user data to the Navigation header of Home Activity?

User does not need to enter username to login, but I wish to get the username from realtime database and pass it to the Navigation Header as well.

Login.java

firebaseAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                progressDialog.dismiss();
                if(task.isSuccessful()){
                    finish();
                startActivity(new Intent(getApplicationContext(),Home.class));

                }
                else
                {

                    Toast.makeText(LoginActivity.this,"Login failed. Kindly check your email and password.",Toast.LENGTH_SHORT);
                }
            }
        }

Home.java

View headerView = navigationView.getHeaderView(0);
    useremail = (TextView)headerView.findViewById(R.id.HVuseremail);
    useremail.setText(Common.currentUser.getName());
    username = (TextView)headerView.findViewById(R.id.HVusername);
    username.setText(Common.currentUser.getName()); 

I expect my navigation header will display the useremail and username on it.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 3
    Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – isalgueiro Jan 21 '19 at 11:08
  • Those ways can be used from textview to textview. However, I would like to retrieve username data from firebase realtime database and I wish to use the "Common.currentUser" as the way to retrieve the data. May I know how to do it? – JiaXian Chan Jan 21 '19 at 11:15
  • what are they using to sign in? – Oush Jan 21 '19 at 11:22

2 Answers2

1

If you have small set of data(like Name, Email) then you can use intent putExtra method suggested by @ Mushirih above.

But if you have bunch of data set then, you can use Android Bundle Intent to pass it in next Activity like below

LoginActivity class

        Bundle bundle = new Bundle();
        bundle.putString("Name",value);
        bundle.putInt("Phone",6752525);
        bundle.putBoolean("IsMale",false);
        //..................like so on ............
        Intent intent = new Intent(LoginActivity.this,SecondActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);

In SecondActivity class you can receive it like :-

Bundle bundle = getIntent().getExtras();
 String showtext = bundle.getString("Name"); //this for string
 int phone = bundle.getInt("Phone"); // this is for phone
  //.....like for other data...............
SAURABH_12
  • 2,262
  • 1
  • 19
  • 19
0

You can pass data across Intents by using the putExtra method.

Intent intent = new Intent(getBaseContext(), Home.class);
intent.putExtra(<your data unique id in quotes>, <your data e.g username>);
startActivity(intent);

In your Home.class you can retrieve your data as below

String username = getIntent().getStringExtra(<your data unique id in quotes>,<default value incase the value is not correctly passed e.g null>);

Hope this answers your question.

Mushirih
  • 451
  • 5
  • 13