I have two separated file which is Login.java and LoginTask.java
My problem is I am trying to pass boolean value "checkFlag" into onPostExecute method .
But the boolean value always null . The code as below :
Login.java
public class Login extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
EditText etUsername, etPassword;
String login_name, login_pass;
CheckBox cbRemember;
public static final String ID="ID";
Boolean checkFlag;
SharedPreferences pref;
SharedPreferences.Editor editor;
final String TAG = this.getClass().getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
etUsername =(EditText)findViewById(R.id.etUsername);
etPassword =(EditText)findViewById(R.id.etPassword);
cbRemember = (CheckBox)findViewById(R.id.cbRemember);
cbRemember.setOnCheckedChangeListener(this);
checkFlag = cbRemember.isChecked();
Log.d(TAG, "Check flag is : " + checkFlag);
pref = getSharedPreferences("login.conf", Context.MODE_PRIVATE);
editor = pref.edit();
}
public void userLogin (View view){
login_name = etUsername.getText().toString();
login_pass = etPassword.getText().toString();
checkFlag = cbRemember.isChecked();
String login_url = getString(R.string.login);
String method = "login";
LoginTask loginTask = new LoginTask(this);
loginTask.execute(method,login_url,login_name, login_pass, String.valueOf(checkFlag));
Toast.makeText(getApplicationContext(), "Check flag value = " + checkFlag, Toast.LENGTH_SHORT).show();
finish();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkFlag = isChecked;
Log.d(TAG, "Check flag is : " + checkFlag);
}
}
LoginTask.java
public class LoginTask extends AsyncTask<String,Void,String>{
public static final String NAME="NAME";
public static final String ID="ID";
private PrefManager pref;
Context ctx;
LoginTask(Context ctx){this.ctx=ctx;}
@Override
protected void onPreExecute(){super.onPreExecute();}
@Override
protected String doInBackground(String... params){}
@Override
protected void onProgressUpdate (Void... values){super.onProgressUpdate(values);}
// I want to pass checkFlag boolean value into this method
@Override
protected void onPostExecute (String result){
String fail = "Try Again";
String login_name = result.trim(); // display "try again" error
String welcome = "Welcome to E-Movement";
pref = new PrefManager(ctx);
if(result.trim().equalsIgnoreCase(fail.trim())){
pref.logout();
Toast.makeText(ctx,"Login failed ! " + fail, Toast.LENGTH_LONG).show();
Intent intent = new Intent(ctx, Login.class);
ctx.startActivity(intent);
}else{
Toast.makeText(ctx, "Check flag is = " + checkFlag, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ctx, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
pref.createLoginSession(login_name);
}}}
new LoginTask(this);
Put all needed parameters as parameter in the constructor. As you do with the context already.
new LoginTask(this, checkFlag);
Other parameters too. Pretty messy how you do it now.
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.