Android: activity has leaked window due to progress dialog? - 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

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.

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.

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

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

Related

Waiting until user actually sees something on the screen

I've got a main Activity, an extra class for my fragment, and inside this fragment is an AsyncTask, which gathers data from various android library (Wifi SSID, BSSID, etc). When I start my app the app shows a blank screen, without any UI. Then after about 2 seconds, the whole data is being shown. I actually want to display my TextViews as "Not connected to a wifi network" in the background, while showing a ProgressDialog until the data is being displayed. I've got the ProgressDialog in my MainActivity, and calling it in my AsyncTask onProgressUpdate
MainActivity.progressDialog = ProgressDialog.show(MainActivity.c,
"ProgressDialog Title",
"ProgressDialog Body");
I'm updating my TextViews in the doInBackground methode (via another methode outside the Fragment)
((Activity) getActivity()).runOnUiThread(new Runnable() {
Would be too big a comment so i'll just put it here.
Sounds like you are using both fragment and AsyncTask in an incorrect way. You should never do anything UI relevant in doInBackground.
Here is an example of what you could do.
I assume the following scenario:
You have a main activity
You have a fragment containing TextViews
You wish to populate the TextViews after loading some data using AsyncTask with a progressDialog
The approach would be to:
Add the fragment in onCreate of your activity (if the fragment is not defined in the layout, then it will automatically be added).
Create the AsyncTask in your fragment like this:
private class LoadData extends AsyncTask<Void, Void, List<String>> {
ProgressDialog progressDialog;
//declare other objects as per your need
#Override
protected void onPreExecute()
{
// getActivity() is available in fragments and returns the activity to which it is attached
progressDialog= new ProgressDialog(getActivity());
progressDialog.setTitle("ProgressDialog Title");
progressDialog.setMessage("ProgressDialog Body");
progressDialog.setIndeterminate(true)
progressDialog.setCancelable(false)
progressDialog.show();
//do initialization of required objects objects here
};
#Override
protected Void doInBackground(Void... params)
{
List<String> results = new ArrayList<String>();
//do loading operation here
//add each of the texts you want to show in results
return results;
}
// onPostExecute runs on UI thread
#Override
protected void onPostExecute(List<String> results )
{
progressDialog.dismiss();
// iterate results and add the text to your TextViews
super.onPostExecute(result);
};
}
Start the AsyncTask in onCreate of your fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
new LoadData().execute();
super.onCreate(savedInstanceState);
}
This way you avoid calling directly back to your activity, which really should not be necessary in your scenario (unless I have misunderstood).
Otherwise please post all the relevant code and layouts.
This line:
I'm updating my TextViews in the doInBackground methode
points to your problem. You need to use the AsyncTask method onProgressUpdate() to publish to the UI thread. You do not call onProgressUpdate() directly, instead you call publishProgress().
Interestingly, I answered a similar question yesterday here: android AsyncTask in foreach
and it includes an example.
Here's what you need to do.
(1) From the place you run the code that gathers data, you should first display the progress dialog. Something like this:
busy = new ProgressDialog (this);
busy.setMessage (getString (R.string.busy));
busy.setIndeterminate (true);
busy.setCancelable (false);
busy.show();
(2) Then you start your data gathering. This must be done in a separate thread (or Runnable). Do something like this:
Thread thread = new Thread ()
{
#Override
public void run()
{
try
{
... gather data ...
Message msg = handler.obtainMessage();
msg.what = LOADING_COMPLETE;
msg.obj = null;
handler.sendMessage (msg);
}
catch (Exception e)
{
Message msg = handler.obtainMessage();
msg.what = LOADING_FAILED;
msg.obj = e.getMessage(); // maybe pass this along to show to the user
handler.sendMessage (msg);
}
// get rid of the progress dialog
busy.dismiss();
busy = null;
}
}
(3) Add a handler to the activity to receive notification when data gathering is complete:
Handler handler = new Handler()
{
#Override
public void handleMessage (Message msg)
{
if (msg.what == LOADING_COMPLETE)
loadingComplete ();
else if (msg.what == LOADING_FAILED)
loadingFailed ((String)msg.obj);
}
};
(4) Implement the handlers:
private void loadingComplete ()
{
...
}
private void loadingFailed (String errorMessage)
{
...
}
That's the essentials.

How to know when my Activity is fully created?

I've an Activity that contains a lot of data and elements.
When it has too information, the activity wait in white for be shown, that is not I want, I actually want to show a activity while it's loading the data.
So, How can I to know when my activity it's fully created?
My intention is to launch in a second thread the activity across the waiting activity.
You should load that data asynchronously, in another thread if it takes that much time to load. Launch another thread (by using an AsyncTask for example of plain Java Threads), load the data and show some loading-View on the Activity that notifies the user that something is happening in the background.
You can use AsyncTask and can show progress dialog till the data is loaded.
private class FetchRSSFeeds extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);
protected void onPreExecute() {
this.dialog.setMessage(getResources().getString(
R.string.Loading_String));
this.dialog.show();
}
protected Boolean doInBackground(final String... args) {
try {
// Fetch the data from URL
// do background process
return true;
} catch (Exception e) {
Log.e("tag", "error", e);
return false;
}
}
#Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (success) {
// Setting data to list adaptar
setListData();
}
}
This method called after on create in activity
#Override
public void onStart() {
super.onStart();
}

How to show ProgressDialog across launching a new Activity?

Goal: Have a ProgressDialog which shows "Loading..." until next Activity is completely loaded and shown on screen.
Tried with ProgressDialog context and activity set to originating Activity. Also tried with getApplicationContext() and getParentContext(). Exceptions on the last two approaches. Need to do this as destination Activity is slow to render due to non-simple Layout file. (Cannot fix that right now due to organizational issues.) Turns out the destination Activity takes 1-2 seconds to OnCreate and then screen goes black for up to 5+ seconds then it paints. The rendering is just slow. Did review with Hierarchy Viewer and see lots of red balls but can't fix now.
Read up on some related but haven't found a fix. E.g. What's the difference between the various methods to get a Context?
E.g. both of these crash. Using the "this" of source Activity doesn't work either.
// Context parentContext = this.getParent().getBaseContext();
Context parentContext = this.getApplicationContext();
ProgressDialogMenuable theProgressDialog = new ProgressDialogMenuable(parentContext,this);
theProgressDialog.setTitle("yeeha");
theProgressDialog.setMessage("weewah");
theProgressDialog.setIndeterminate(true);
theProgressDialog.setCancelable(true);
theProgressDialog.show();
Also, oddly, nothing happens when I do this:
theProgressDialog.show();
ActivityHelper.changeActivity(this, v, InsMyHoldingsActivity.class, extraMap, -1, -1);
User clicks button to show next activity but the ProgressDialog conflicts with the Activity launch and nothing actually happens other than the button becoming yellow ontouch. Button below works. removing ProgressDialog creation and it works. No console messages logged. A little offputting to the developer for sure.
You can show a progress dialog like this -
Define this
private ProgressDialog pd = null;
in your activity class
Put this in your onCreate (Dont setContentView directly here)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.pd = ProgressDialog.show(this, "Fancy App",
"Loading...Please wait...", true, false);
// Start a new thread that will download all the data
new IAmABackgroundTask().execute();
}
// Background heavy lifting
class IAmABackgroundTask extends
AsyncTask<String, Integer, Boolean> {
#Override
protected void onPreExecute() {
// showDialog(AUTHORIZING_DIALOG);
}
#Override
protected void onPostExecute(Boolean result) {
// Pass the result data back to the main activity
ActivityName.this.data = result;
if (ActivityName.this.pd != null) {
ActivityName.this.pd.dismiss();
}
setContentView(R.layout.main);
}
#Override
protected Boolean doInBackground(String... params) {
//Do all your slow tasks here but dont set anything on UI
//ALL ui activities on the main thread
return true;
}
}
Also go through this :http://developer.android.com/training/improving-layouts/index.html for optimizing layout performance.
Also Use Traceview to look for bottlenecks
There is two ways to
First approach To use Async Task
If you are doing heavy tasks eg loading data from server or parsing xml in that case use AsynTask<> If you want to call ActivityB from ActivityA then
*step-1*create a AsyncTask class. write all background tasks inside doBackground() method and after completion of task you want to call an activity that code write inside onPostExecute() post execute method
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
public class LoadingDataFromServer extends AsyncTask {
Context currentContext = null;
boolean isCancelled = false;
public LoadingDataFromServer(Context context) {
currentContext = context;
}
#Override
protected void onPreExecute() {
if (DashboardActivity.progressBarLayout != null) {
DashboardActivity.progressBarLayout.setVisibility(View.VISIBLE);
// Log.i(TAG,".....Now make progress bar visible.....");
}
super.onPreExecute();
}
#Override
protected Object doInBackground(Object... params) {
// do background processing
try {
// do background tasks eg sever communication
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
// progressDialog.dismiss();
// call second Activity
Intent i = new Intent(currentContext, com.ActvityB.class);
super.onPostExecute(result);
}
#Override
protected void onCancelled() {
// TODO Auto-generated method stub
isCancelled = true;
super.onCancelled();
}
}
step-2 In the activity fro where you want to jump to new activity (eg in ActivityA) call the execute() of AsynTask
new LoadingDataFromServer(context).execute(null);
Second approach
First show progress dialog.
create a thread to do all background tasks. when the thread completes the task then cancel the progress dialog and call the next activity
or
when thread complets the task then call next activity pass this object (progress dialog) and inside that new activity dismiss this dialog.
Here is my code that can help.
In this, I'm only posting the first method of AsyncTask i.e onPreExecute.
User_AsyncTask extends AsyncTask:
public class User_AsyncTask extends AsyncTask<String, String, String>
{
String response = "";
#Override
protected void onPreExecute()
{
try
{
if (progressDialog != null)
progressDialog.cancel();
}
catch (Exception e)
{
}
progressDialog = ProgressDialog.show(DisplayDetails.this, "", "Please wait...", true, true);
progressDialog.setCancelable(false);
progressDialog.show();
}

doInBackground calling AsyncTask then sleeps blocks UI Thread

I have an activity, composed of an AsyncTask aiming to launch a request when the user clicks on the button. I have been looking for answers, but I didn't find the same problem, or it didn't the same for me. The code is doing what I want, but the ProgressDialog looks blocked as the spinner is not turning sometimes (almost all the time).
When I click on the button :
AsyncTask is launched -> showDialog() is called onPreExecute -> startSearch ( SearchManager launches a new AsyncTask with in the doInBackground there is a heavy call with network ) -> doInBackground in Activity waits for SearchManager to be loaded -> display.
Code for button :
button_search.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new SearchTask().execute();
}
});
Code for AsyncTask in Search Activity :
private class SearchTask extends AsyncTask<Void,Void,Void>{
#Override
protected void onPreExecute(){
showDialog(DIALOG_LOADING_ID);
searchManager.startSearch();
}
#Override
protected Void doInBackground(Void... params) {
while(searchManager.isLoading()){
try {Thread.sleep(150);} catch(Exception e){};
}
return null;
}
#Override
protected void onPostExecute(Void ret){
try {dismissDialog(DIALOG_LOADING_ID);} catch (Exception e){};
if ( searchManager.errorOccurred() ){
//Error
} else {
//No Error
}
}
Code for SearchManagerAsyncTask : which is directly launched by startSearch
protected class SearchAsync extends AsyncTask <Void,Void,Void>{
#Override
protected Void doInBackground(ComSearchAds... datas) {
global.getDataManager().doSearch();
//... When finished
setIs_loading(false);
}
}
I'm apparently doing something wrong, but can't find what and how to avoid this. Thanks for your help !
SOLUTION :
Finally, it appears that the not spinning ProgressDialog was because I was using the same instance of ProgressDialog and
showDialog(DIALOG_LOADING_ID);
//doInBackground
dismissDialog(DIALOG_LOADING_ID);
used with causes problem, I changed to
removeDialog(DIALOG_LOADING_ID)
and now it's working fine.
Thanks All, and hope it can help someone someday !
You don't need to create another task , just instead of doing the search stuff via another activity.
all you need to do is to put you search cod ein doInbackGround() of search task. e.g
#Override
protected Void doInBackground(Void... params) {
global.getDataManager().doSearch();
return null;
}
also use class level variable to get search result to store true/false
#Override
protected void onPostExecute(Void ret){
// Set you result here
setIs_loading(my_searh_result);
try {dismissDialog(DIALOG_LOADING_ID);} catch (Exception e){};
if ( searchManager.errorOccurred() ){
//Error
} else {
//No Error
}
}
Could you please try to add a ProgressBar control in your layout file and set its Visible to true when starting async task and set to gone when end of process reached. Below is the code for the same.
<ProgressBar
android:layout_width="wrap_content"
android:id="#+id/progressBar1"
android:layout_height="wrap_content"></ProgressBar>
It's hard to say, but try this for onClickListner:
button_search.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new SearchTask().execute();
setIs_loading(true);
searchManager.startSearch();
}
});
It's a pretty wild guess, but it might work

AsyncTask not locking main thread?

I'm using following code to fill a custom ListPreference dialog. Since the fill procedure takes a lot of time i want to show a progress dialog during the fill procedure.
My problem is that filler.execute() does not block onPrepareDialogBuilder and functions goes till the end before values are filled causing an exception... Any idea?
#Override
protected void onPrepareDialogBuilder(Builder builder) {
// Load data
if (this.getEntries()==null) {
FillerTask filler = new FillerTask();
filler.execute();
}
Log.d(TAG, "Filler finished");
super.onPrepareDialogBuilder(builder);
}
Here is Filltertask code, basically he looks for every activity with a MAIN Intent filling a list:
private class FillerTask extends AsyncTask<Void, Void, String[][]> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
Log.d(TAG, "Dismiss dialog");
dialog = ProgressDialog.show(MyListPreference.this.getContext(), "", "Doing stuff...", true);
}
#Override
protected String[][] doInBackground(Void... params) {
return fill();
}
public String[][] fill() {
Log.d(TAG, "Fill started");
CREATE LISTS...
// Done
Log.d(TAG, "Fill done");
String[][] result = new String[][] {entryNames, entryValues};
return result;
}
#Override
protected void onPostExecute(String[][] result) {
Log.d(TAG, "Post execute");
MyListPreference.this.setEntries(result[0]);
MyListPreference.this.setEntryValues(result[1]);
dialog.dismiss();
}
}
My problem is that filler.execute() does not block onPrepareDialogBuilder and functions goes till the end before values are filled causing an exception... Any idea?
That is the entire point behind an AsyncTask. The "Async" in AsyncTask means asynchronous.
Use your AsyncTask to get your data. Then, in onPostExecute(), display the dialog.
Found the solution, best way to do this is override the onClick method and let the AsyncTask postExecute call the "super()", so click is not passed until content is loaded and during load progress bar is correctly displayed.
asyntask doesn't lock main thread, it just drops a message to message queue of main thread

Categories

Resources