简体   繁体   English

如何将数据从一个活动传递到下一个活动

[英]How to pass the data from one Activity to the next activity

I'm currently developing an android app and using firebase realtime database. 我目前正在开发一个android应用,并使用firebase实时数据库。 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? 为了将用户数据传递到Home Activity的Navigation标头中,我应该在Login 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 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 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. 我希望我的导航标题将在上面显示useremail和用户名。

If you have small set of data(like Name, Email) then you can use intent putExtra method suggested by @ Mushirih above. 如果您的数据集很少(如名称,电子邮件),则可以使用上述@ Mushirih建议的intent putExtra方法。

But if you have bunch of data set then, you can use Android Bundle Intent to pass it in next Activity like below 但是,如果您设置了一堆数据,则可以使用Android Bundle Intent在下一个Activity中传递它,如下所示

LoginActivity class LoginActivity类

        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 :- 在SecondActivity类中,您可以像这样接收它:

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

You can pass data across Intents by using the putExtra method. 您可以使用putExtra方法跨Intent传递数据。

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 在您的Home.class中,您可以按以下方式检索数据

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. 希望这能回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM