Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: activity has leaked window due to progress dialog?

Tags:

android

I am developing my first Android App and I want to display progress dialog while user click on login button in my apps. so I integrated asynctask in apps, all operation like login logout successfully done but problem is that after successfully login this giving me error like LoginActivity has leaked window due to progress dialog. how to dismiss progress dialog and update the UI.

please refer following code and tell me some changes

following is the LoginActivity

public class LoginActivity extends SherlockActivity {
.................
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    sessionmngr = new SessionManager(this);
    //check the user login or not
    if (sessionmngr.isLoggedIn()) {
        Intent checkLoginIntnt = new Intent(this,ProjectFragActivity.class);
        startActivity(checkLoginIntnt);
    }
    setContentView(R.layout.activity_login);
    ........
}
// onclick listener when click on login activity

public void LoginToBookingScape(View v) throws JSONException {

    username = edtTxtUserName.getText().toString();
    userpsw = edtTxtUserPsw.getText().toString();

    if ((username.trim().length() > 0)&&(userpsw.trim().length() > 0)) {

        JsonWebService jsonWebs = new JsonWebService();
        jsonWebs.execute(loginUrl);

    }else {
        ............
    }   
}

Following is the Inner class to extend AsyncTask in LoginActivity

private class JsonWebService extends AsyncTask<String,Void,String> {

    private ProgressDialog dialogLogin;
    @Override
    protected String doInBackground(String... url) {

                httpPost.setEntity(new UrlEncodedFormEntity(params));
                ....
                inStream = httpEntity.getContent();
                .........
                return jsonResp;
    }

    @Override
    protected void onPostExecute(String jsonData) {
        //get string data from doinBackground
        try {
            JSONObject jsonObj = new JSONObject(jsonData);
            String key_login = jsonObj.getString(KEY_LOGIN);

            if (key_login.equalsIgnoreCase("0")) {
                .............

            }else {
                ....
                sessionmngr = new SessionManager(getApplicationContext());

                sessionmngr.createLoginSession(id,jsonObj.getString(KEY_UNAME), 
                        jsonObj.getString(KEY_UEMAIL));

                dialogLogin = ProgressDialog.show(LoginActivity.this, "Bookingscape", 
                        "Please Wait",true);
                dialogLogin.setIcon(R.drawable.icon);
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                Intent inteProj = new Intent(getApplicationContext(),           

                    ProjectFragActivity.class);
                startActivity(inteProj);
                finish();   
            }
        ........
    }
    @Override
    protected void onCancelled() {
        dialogLogin.dismiss();
        dialogLogin = null;
        super.onCancelled();
    }
}

}

I want ask one question here

Is above code optimize and reusable.

Thanks in advance

like image 461
Nilesh Avatar asked Dec 21 '22 06:12

Nilesh


1 Answers

The problem is you are moving to new activity without dismissing the progress dialogue . this will cause leaked window error

I think you must move dialogLogin.dismiss(); form onCancelled()block to onPostExecute block in your code

you must do this before you are going to another activity . ie before

Intent inteProj = new Intent(getApplicationContext(),ProjectFragActivity.class);
startActivity(inteProj);

this line of code .I think this will solve your issue

one doubt : where is your onPreExecute ?? Usually i display progress code in that block and dismiss that in onPostExecute

Usually the flow is like this onPreExecute-->doInBackground --->onPostExecute

EDIT :

onPreExecute: Initialize your UI components (eg: Dialoges) .In your case ProgressDialog showed up

doInBackground : After onPreExecute block control goes to this block this will .Here the ProgressDialog continues it's work

onPostExecute : control come here after all background action .Here you can dismiss your ProgressDialog and goto your new activity.

like image 130
edwin Avatar answered Mar 16 '23 07:03

edwin