JiaXian Chan
JiaXian Chan

Reputation: 25

How to pass the data from one Activity to the next activity

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.

Upvotes: 0

Views: 1230

Answers (2)

SAURABH_12
SAURABH_12

Reputation: 2310

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...............

Upvotes: 1

Mushirih
Mushirih

Reputation: 451

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.

Upvotes: 0

Related Questions