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