java - 如何将数据从一个活动传递到下一个活动
问题描述
我目前正在开发一个 android 应用程序并使用 firebase 实时数据库。如何将用户数据从登录活动传递到主页活动的导航标题?
为了将用户数据传递给 Home Activity 的 Navigation 标头,我应该在 Login Activity 中添加什么?
用户无需输入用户名即可登录,但我希望从实时数据库中获取用户名并将其传递给导航标题。
登录.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);
}
}
}
主页.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());
我希望我的导航标题将在其上显示 useremail 和用户名。
解决方案
如果您有少量数据(如姓名、电子邮件),那么您可以使用上面@Mushirih 建议的意图 putExtra 方法。
但是如果你有一堆数据集,你可以使用 Android Bundle Intent 将它传递给下一个活动,如下所示
登录活动类
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);
在 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...............
推荐阅读
- encoding - GitAhead:如何在差异视图中设置正确的文件编码?
- c# - SmartCardReader (NFC RFID) 在 Kioskmode UWP 中不工作
- javascript - URL 路径匹配的正则表达式不起作用。如何允许某些路线?
- vue.js - How to use GraphQL queries with BootstrapVue b-table?
- c++ - Append to ostream in a preprocessor macro
- typo3 - Optional argument in Extbase RouteEnhancer
- javascript - 网页 DOM 渲染速度随着时间的推移而降低并导致浏览器冻结
- javascript - How does one create a function to call itself when a given condition is true in Mithril.js
- java - Verify SMTP server authentication via java api (Healthcheck including AUTH)
- python - how to get Wireless LAN adapter Wi-Fi IP Address in Python?