I wanted to pass a data to CUpdateAccountActivity class from LoginActivty class, however when I go to CUpdateAccountActivity class, the value is lost, but when I check in LoginActivity class, it has the value, but can't pass it to another class, can someone have any suggestion? I don't know what happened, it seems it cant pass the data, all I receive from CUpdateAccountActivity class is Null.
public class LoginActivity extends AppCompatActivity {
EditText userlogtxt, passlogtxt;
Button loginlogbtn;
DBHelper myDB;
CBuyFuelActivity cb;
CUpdateAccountActivity cp;
public String username1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userlogtxt = findViewById(R.id.userlogtxt);
passlogtxt = findViewById(R.id.passlogtxt);
loginlogbtn = findViewById(R.id.loginlogbtn);
myDB = new DBHelper(this);
cb = new CBuyFuelActivity();
cp = new CUpdateAccountActivity();
loginlogbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = userlogtxt.getText().toString();
String password = passlogtxt.getText().toString();
String type1 = "admin";
String type2 = "driver";
String type3 = "client";
if(username.equals("") || password.equals("")) {
Toast.makeText(LoginActivity.this, "Enter Credentials", Toast.LENGTH_SHORT).show();
}
else {
Boolean result = myDB.checkAccifAdmin(username,password);
Boolean result2 = myDB.checkAccifDriver(username,password);
Boolean result3 = myDB.checkAccifClient(username,password);
if(result == true) {
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "Welcome Admin!", Toast.LENGTH_SHORT).show();
Toast.makeText(LoginActivity.this, "" + cb.cusername + cb.cpassword, Toast.LENGTH_SHORT).show();
}
else if(result2 == true) {
Toast.makeText(LoginActivity.this, "Welcome Driver!", Toast.LENGTH_SHORT).show();
}
else if(result3 == true) {
username1 = userlogtxt.getText().toString();
cp.name1 = username1;//I AM STARTING TO PASS THE DATA HERE
Intent intent = new Intent(getApplicationContext(), CHomeActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "Welcome Client!" +cp.name1, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(LoginActivity.this, "Invalid Credentials", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
public class CUpdateAccountActivity extends AppCompatActivity {
DBHelper myDB;
EditText cfirstnametxt, clastnametxt, cusernametxt, cpasswordtxt, ccontacttxt, cagetxt;
Button csavebtn;
LoginActivity lg;
public String name1; //I AM EXPECTING TO RECEIVE A DATA FROM LoginActiviy CLASS AND STORE IT IN A NAME1 STRING VARIABLE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cupdate_account);
lg = new LoginActivity();
myDB = new DBHelper(this);
//cp = new CUpdateAccountActivity();
cfirstnametxt = findViewById(R.id.cfirstnametxt);
clastnametxt = findViewById(R.id.clastnametxt);
cusernametxt = findViewById(R.id.cusernametxt);
cpasswordtxt = findViewById(R.id.cpasswordtxt);
ccontacttxt = findViewById(R.id.ccontacttxt);
cagetxt = findViewById(R.id.cagetxt);
csavebtn = findViewById(R.id.csavebtn);
viewAccData();
csavebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(CUpdateAccountActivity.this, ""+name1, Toast.LENGTH_SHORT).show();
}
});
}
You can pass value(s) to an activity being started by startActivity(the_intent) via Intent extras.
The following is a simplified version of you code. The initial activity is LoginActivity. This just has a button, that when clicked starts the CUpdateAccountActivity with two values (a String and a Long) passed from the LoginActivity. The values are extracted from the Intent and then a) displayed in a TextView, b) written to the log and c) Toasted.
First LoginAactivity :-
public class LoginActivity extends AppCompatActivity {
private Button loginBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginBtn = this.findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this,CUpdateAccountActivity.class);
/* use the intent extra keys as per the CUpdateAccountActivity class */
/* passed values hard coded for brevity of demo */
intent.putExtra(CUpdateAccountActivity.IE_USERNAMEKEY,"A String value to be passed");
intent.putExtra(CUpdateAccountActivity.IE_USERID,100L);
startActivity(intent);
}
});
}
}
Second CUpdateAccountActivity :-
public class CUpdateAccountActivity extends AppCompatActivity {
/* IntentExtra Keys - public so can be accessed by other classes */
public static final String IE_USERNAMEKEY = "ie_usernameKey";
public static final String IE_USERID = "ie_userid";
String passedUserName = "";
long passedUserId;
TextView passedValues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cupdate_account);
passedValues = this.findViewById(R.id.passedValues);
/* Get the passed values */
passedUserName = this.getIntent().getStringExtra(IE_USERNAMEKEY);
passedUserId = this.getIntent().getLongExtra(IE_USERID,-1);
/* Use the passed values
a) in the TextView
b) written to the log
c) via Toast
* */
passedValues.setText("Login Activity passed > "
+ passedUserName + "< via extra " + IE_USERNAMEKEY
+ "< and >"
+ passedUserId + "< via extra " + IE_USERID
);
Log.d("PASSEDVALUE","Login Activity passed >" + passedUserName +"< via extra >" + IE_USERNAMEKEY);
Log.d("PASSEDVALUE","Login Activity passed > " + passedUserId + "< via extra >" + IE_USERID);
Toast.makeText(
this,
"Loging Activity passed >" + passedUserName + "< via extra " + IE_USERNAMEKEY
+ " and >" + passedUserId + "< via extra " + IE_USERID,
Toast.LENGTH_SHORT
).show();
}
}
When start the demo App displays :-
Clicking the LOGIN button results in :-
The toast is also displayed and the log includes :-
2021-10-22 15:55:55.509 D/PASSEDVALUE: Login Activity passed >A String value to be passed< via extra >ie_usernameKey
2021-10-22 15:55:55.509 D/PASSEDVALUE: Login Activity passed > 100< via extra >ie_userid
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.