4

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

Nilesh
  • 93
  • 1
  • 2
  • 7
  • generally you display progress dialog in onPreExecute() do some background work in doInBackground() and in onPostExecute() dismiss the dialog and update ui – Raghunandan Apr 23 '13 at 09:33

4 Answers4

11

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.

edwin
  • 7,985
  • 10
  • 51
  • 82
  • edwin,i am not written onPreExecute method. – Nilesh Apr 23 '13 at 09:59
  • @Nilesh OK i just ask ..No worries ..Did this solve you issue ?? – edwin Apr 23 '13 at 10:04
  • edwin i wrote dialogLogin.dismiss() in a thread block(after Thread.sleep(4000)) but giving me error like IllegalStateException : View not attached to the windowManager. that's why i wrote dialogLogin.dismiss() in onCanCelled method.how to fix it? – Nilesh Apr 23 '13 at 10:09
  • @Nilesh create onPreExecute block and initialize your dialog inside that . then dismiss it in onPostExecute – edwin Apr 23 '13 at 10:11
  • thanks edwin its work. can you explain how it work? one more question i want to ask edwin is in above code i wrote if(sessionmngr.isLoggedIn() on top line bcoz check user already login, when user click on the back button from second activity then user render to second activity but its not working. can you help me if you time. thanking you – Nilesh Apr 23 '13 at 10:43
  • when user click on the back button from second activity then user render to second activity but its not working. Sorry Actually i didn't understand what you mean here ..Do you mean the user must not got to login page agin on back press ?? – edwin Apr 23 '13 at 10:53
  • so actually what you want to happen when user press back button ?? in your LoginActivity use finish() after initializing checkLoginIntnt after that startActivity(checkLoginIntnt);. but i have doubt if you didi this , when you press back button you will get exit of your app . – edwin Apr 23 '13 at 11:00
  • @Nilesh is this ok with you – edwin Apr 23 '13 at 11:09
  • no edwin, this is not working again goes to second activity. ok edwin i will do that another way. thank you for your help. – Nilesh Apr 23 '13 at 11:16
  • @Nilesh actually what do you want if press back button is second activity ?? – edwin Apr 23 '13 at 11:22
  • please check this question http://stackoverflow.com/questions/16082494/android-how-to-maintain-session-on-back-button – Nilesh Apr 23 '13 at 11:41
0

Possibly Its Because of you are writing

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

before dismissing dialog, means your dialog is still showing process even your activity changed. so just put your

dialogLogin.dismiss();
dialogLogin = null;

lines before this

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

then this problem will get resolved.

Lalit Jawale
  • 1,216
  • 9
  • 19
0

I had this issue also and this is what caused it. My app takes input and adds them to SQLite database. This is what I had:

 public Item doInBackground(String...params) {
       Item item = new Item();
        try {

            item.setItemName(params[0]);
            item.setSupplierPhone(params[1]);
            ...
            databaseHandler.addItem(item);

            //Toast.makeText(mContext, "Item successfully saved.", Toast.LENGTH_SHORT).show();

            databaseHandler.close();


        } catch (SQLiteException e) {
            Toast.makeText(mContext, "Error saving Item!", Toast.LENGTH_SHORT).show();
        }
        return item;
    }

I think this was because I was trying to show a Dialog after execution path was already in onPostExecute()

It is also important to note that there are a lot of reasons why this exception is thrown. A lot of them are discussed here

Community
  • 1
  • 1
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
0

You need to dismiss the dialog before forwarding to next activity. Use:- dialog.dismiss();

Mayur Waghmare
  • 61
  • 1
  • 1
  • 4