Question:
I am working on an android application which performs an authentication through an actividad "A"
( LoginActivity ), this instantiates the actividad "B"
( LoginTask ) that inherits from AsyncTask to carry out said process, once the authentication is done it is instantiated to actividad "C"
( PrincipalActivity ), being something like this A -> B -> C.
Located in actividad "C"
, I press the "Back" button and it "takes" me out of the main activity and places me in actividad "A"
, that is, it returns me to the login screen. Here my code:
LoginActivity
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initUI();
}
private void initUI() {
Typeface font = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/NotoSans-Regular.ttf");
final EditText txtUser = (EditText)findViewById(R.id.txtUser);
txtUser.setTypeface(font);
final EditText txtPass = (EditText)findViewById(R.id.txtPass);
txtPass.setTypeface(font);
Button btnAccess = (Button) findViewById(R.id.btnAccess);
btnAccess.setTypeface(font);
btnAccess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Login loginBean = new Login();
String user = txtUser.getText().toString().trim();
String pass = txtPass.getText().toString().trim();
if (!user.equals("")) {
if(!pass.equals("")) {
loginBean.setUser(user.toUpperCase());
loginBean.setPass(pass);
} else {
txtPass.setError(getString(R.string.err_pass));
return;
}
} else {
txtUser.setError(getString(R.string.err_user));
return;
}
LoginTask loginTask = new LoginTask(LoginActivity.this,loginBean);
loginTask.execute();
}
});
}
}
LoginTask
public class LoginTask extends AsyncTask {
private ProgressDialog progressDialog;
private Context context;
private Resources resources;
private Login loginParams;
private String msgAccess;
public LoginTask(Context context, Login loginParams) {
this.context = context;
this.resources = context.getResources();
this.loginParams = loginParams;
}
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage(resources.getString(R.string.msg_access));
progressDialog.setCancelable(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
}
@Override
protected String doInBackground(Object[] objects) {
//Autenticación
return msgAccess;
}
protected void onPostExecute(Object o) {
if (msgAccess != null) {
if (msgAccess.equals("OK")) {
Intent intent = new Intent(context, PrincipalActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
((Activity)context).finish();
} else {
Toast.makeText(context, msgAccess, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(context, R.string.err_msg_null, Toast.LENGTH_LONG).show();
}
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
So, what I had thought was to perform some kind of validation in the onBackPressed()
event of my main class so that when I press the "Back" button I don't go back to "A", but stay in the main activity "C" :
@Override
public void onBackPressed() {
if (LoginPersistence.getStatus() > 0 && !LoginPersistence.getToken().equals("")) {
//Agregar aqui la validación
}
}
To be more explicit, what I indicate is that when I am in my main activity "C" and I press the "Back" button, it returns me to activity "A", that is, to enter the username and password again, when it should not. do it. Pressing the "Back" button should "minimize" my app and when I go back in it should keep it in the main activity. This is what I do when I press the "Home" button, it minimizes the application and when I return, it loads me where I left off and I achieve this with intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
.
I hope I have explained myself. Thank you.
Answer:
Based on your question, you want to return to "activity B", LoginTask
but remember that this isn't actually an Actividad
it's an AsyncTask
.
- "A" (LoginActivity)
- "B" (LoginTask)
- "C" (MainActivity)
LoginTask
is a Thread
that actually performs an Intent
to open the PrincipalActivity
.
Intent intent = new Intent(context, PrincipalActivity.class);
If you want to return to "B" LoginTask
, then this class should extend Activity
and within this class an instance of the Asynctask
with the same functionality.