0

Hi I am getting force close in my applcation.

Error - View Not attached to window manager.

//asynctask for groupnames
    class GroupDataLoad extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            mCustomProgressDialog = CustomProgressDialog.createDialog(
                    UserMenuActivity.this, "", "");

            mCustomProgressDialog.show();   
            mCustomProgressDialog.setCancelable(false); 
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            dbHelper = new DatabaseHelper(UserMenuActivity.this);
            //getting the group list
            mXMPPConn.getContactList();

            ParseValues.parsedGroupList.clear();
            APIVariables apiVariables = new APIVariables();
            ParseValues.getGroupList(apiVariables.getGroupList("abc.com"));

            int size = ParseValues.parsedGroupList.size();
            for(int i=0;i<size;i++){
                String groupName = ParseValues.parsedGroupList.get(i).getGroup_name();
                if(CGMStaticVariable.CommonConnection.isConnected())
                {
                    createRoom(groupName, CGMStaticVariable.CommonConnection);
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            setExpandableListView();
            mCustomProgressDialog.dismiss();    
        }
    }

When loading starts, I change the forground page by clicking on the notification from the status bar and when I click BACK button to come to the same activity again, it crashes saying - "View not attached to window manager"

I am unable to figure out what the problem actually is

Thanks

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143
  • 1
    http://stackoverflow.com/questions/1111980/how-to-handle-screen-orientation-change-when-progress-dialog-and-background-thre – βhargavḯ Mar 04 '13 at 06:44
  • Could you add some more clarifications about the activity that starts the dialog and it's life cycle? I had a similar issue and the problem was that the activity was destroyed(closed) before onPostExecute(), where the dialog is dismissed – stan0 Mar 04 '13 at 10:21
  • @stan0 Yes my problem is the same, I am destroying the activity before onPostExecute is called, then what is the solution ? – Gaurav Arora Mar 04 '13 at 10:29

1 Answers1

0

I think I did something else back then, but if I had to fix it now, I'd create the progress dialog in the activity when the asynctask is started. I'd keep a reference to the dialog in the activity and dismiss it after the task has finished. In order to tell the activity that the task has finished:

  1. create an interface
  2. make the activity implement the interface
  3. make the async task receive and keep a week reference to an object that implements the interface
  4. pass the activity ("this") to the task
  5. in onPostExecute() call the interface method of the passed object
  6. in the implementation of that method, destroy/dismiss the dialog

In addition, if the activity is destroyed before the task has finished, the dialog will be destroyed too. It's important to use weak reference, so that the activity is not referenced until the potentially long task has finished.

stan0
  • 11,549
  • 6
  • 42
  • 59