Easy use EventBus.
Add EventBus to your project
implementation("org.greenrobot:eventbus:3.3.1")
Create a class to store the data you want to send
public class User {
private String theBank;
private int userId;
private String password;
private String bankView;
private String errorLogin;
public User(String theBank, int userId, String password, String bankView, String errorLogin) {
this.theBank = theBank;
this.userId = userId;
this.password = password;
this.bankView = bankView;
this.errorLogin = errorLogin;
}
public String getTheBank() {
return theBank;
}
public void setTheBank(String theBank) {
this.theBank = theBank;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBankView() {
return bankView;
}
public void setBankView(String bankView) {
this.bankView = bankView;
}
public String getErrorLogin() {
return errorLogin;
}
public void setErrorLogin(String errorLogin) {
this.errorLogin = errorLogin;
}
}
In the activity/fragment that received the data
Register and unregister your subscriber
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
Message will be received in here
private User _user;
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(User user) {
Log.d("theBank", user.getTheBank());
Log.d("userId", user.getUserId());
Log.d("password", user.getPassword());
Log.d("bankView", user.getBankView());
Log.d("errorLogin", user.getErrorLogin());
// save user in member variable (_user),
// so you can access _user from other place in this activity or fragment
_user = user;
}
In the activity/fragment that sends the data
User user = new User();
user.setTheBank = "my bank";
user.setUserId = 1;
user.setPassword = "my password";
user.setBankView = "my bank view";
user.setErrorLogin = "my error login";
EventBus.getDefault().post(user);