Search code examples
androidandroid-intentandroid-asynctaskactivity-finish

Finish the activity intent in asynytask


hi im currently working on android mysql for my libary mobile application but How can i close the log in activity after i log in , context .finish(); is not working here's my codes.i know in normal intent its only finish(); but how can i do it in my code.

my asyntask:

public class backgroundWorker extends AsyncTask<String,Void,String> {

    Context context;
    AlertDialog alertDialog;

    String result;

    backgroundWorker(Context ctx) {
        context = ctx;
    }

    ProgressDialog progressDialog;

    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://192.168.254.120/LibrayAPI/Signin.php";

        if (type.equals("login")) {


            String user_name = params[1];
            String password = params[2];


            try {

                String data = URLEncoder.encode("user_name", "UTF-8") + "=" +
                        URLEncoder.encode(user_name, "UTF-8");
                data += "&" + URLEncoder.encode("password", "UTF-8") + "=" +
                        URLEncoder.encode(password, "UTF-8");
                URL url = new URL(login_url);
                URLConnection conn = url.openConnection();

                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter((conn.getOutputStream()));

                wr.write(data);
                wr.flush();

                BufferedReader reader = new BufferedReader(new
                        InputStreamReader(conn.getInputStream()));

                StringBuilder sb = new StringBuilder();
                String line;
                //Read server response
                while ((line = reader.readLine()) != null) {

                    sb.append(line);
                    break;
                }
                return sb.toString();

            } catch (Exception e) {
                return new String("Exeption" + e.getMessage());
            }
        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Log in Status");
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("\tLoading");
        progressDialog.show();
    }

    @Override
    protected void onPostExecute(String result) {
         progressDialog.dismiss();
        alertDialog.setMessage(result);
        alertDialog.show();

        if (result.equals("Log in sucessful")) {
            alertDialog.dismiss();
            Intent todash = new Intent(context, DashBoard.class);
            context.startActivity(todash);

        } else {
        }

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

Log in Activity:

public class Login extends AppCompatActivity {

    EditText username,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username = findViewById(R.id.edtUsername);
        password = findViewById(R.id.edtPassword);
    }

    public void onLogin(View view) {

        String Username = username.getText().toString();
        String Password = password.getText().toString();
        //notif call
//call notif for overdue tommorow
        backWorkerNotifTom backWorkerNotifTom = new backWorkerNotifTom(this);
        //get datetime tom
        Calendar calendar =  Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,1);
        Date dateTom = calendar.getTime();
        SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
        String dateTomString = sf1.format(dateTom);
        Toast.makeText(this, dateTomString, Toast.LENGTH_SHORT).show();
        backWorkerNotifTom.execute("SelectNotifTom",dateTomString,Username);

        backWorkerNotif backWorkerNotif = new backWorkerNotif(this);
        Date date = Calendar.getInstance().getTime();
        SimpleDateFormat SF = new SimpleDateFormat("yyyy-MM-dd");
        String DateNow = SF.format(date);
        backWorkerNotif.execute("Notif", DateNow, Username);

        String Type = "login";
        GlobalVariable.BorrowerID = Username;
        GlobalVariable.Password = Password;
        backgroundWorker _backgroundWorker = new backgroundWorker(this);
        _backgroundWorker.execute(Type, Username, Password);


    }
}

PS: looks like im new to android.


Solution

  • Try use Activity instead of Context

    Activity context;
    AlertDialog alertDialog;
    
    String result;
    
    backgroundWorker(Activity ctx) {
        context = ctx;
    }
    

    Give Login.this isntead of context when you call backgroundWorker

    backgroundWorker _backgroundWorker = new backgroundWorker(Login.this);