Reputation: 93
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
Upvotes: 4
Views: 15411
Reputation: 71
You need to dismiss the dialog before forwarding to next activity. Use:- dialog.dismiss();
Upvotes: 0
Reputation: 27237
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
Upvotes: 0
Reputation: 1246
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.
Upvotes: 0
Reputation: 8081
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.
Upvotes: 11