I have a simple AsyncTask whose doInBackground() calls a function fn1 which in turn simply calls another fn2. The result is a “Activity has leaked window” crash! When I call fn2 directly from doInBackground() everything works fine. Does that mean in a thread one cannot have more than 1 level of fn calling?( Thread-> fn1->fn2)
I am new to Java and android, so please bear with me if that is a basic question! Thnx.
My code algo is something ike this :
MyActivity:
MyLibrary myLib;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myXML);
myLib = new MyLibrary(MyActivity.this);
new myTask().execute();
}
private class myTask extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(MyActivity.this, "", "Fetching Data...");
}
@Override
protected Void doInBackground(Void... params) {
myLib.fn1();
return null;
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
}
}
MyLibrary :
Context ctx;
String iVar;
public MyLibrary(Context context){
this.ctx = context;
}
public void fn1()
{
fn2();
Log.d("TAG", "Function 1";
}
public void fn2()
{
iVar = "100";
Log.d("TAG", "Function 2";
}
Add the following in your Activity’s code:
It will dismiss the dialog window in case you move away from activity.