0

I start a service on user login which has a functionality to download many attachments in background using AsyncTask.

While the background download of attachments is in progress and user logged in to the application, I rotate my device from landscape to portrait orientation.

In portrait orientation, I have another functionality to calculate some data in background using AsyncTask. This functionality contains following code -

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Calculating...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Integer doInBackground(Integer... args) {
        //Some data processing

    }

    @Override
    protected void onPostExecute(Integer file_url) {
        super.onPostExecute(file_url);

        if(pDialog.isShowing()) {
            pDialog.dismiss();
            pDialog.cancel();
        }

    }

The problem is -

When I rotate the device to portrait orientation -

1) The progress dialog never gets dismissed until I restart/relaunch the application.

2) The portrait layout waits for the attachments background task to get complete and only then it calculates the async data which is in its own layout in background.

3) When the calculation of async data gets completed, the portrait layout is filled with that calculated data but the progress dialog still doesn't gets dismissed until app restart.

EdmDroid
  • 1,350
  • 1
  • 11
  • 25
sjain
  • 23,126
  • 28
  • 107
  • 185
  • 2
    when the activity got destroyed so did the `pDialog` reference and it was recreated I making the AsyncTask have a reference to a variable that dos not exist – tyczj Mar 31 '14 at 16:06

2 Answers2

1

You've got to remember that on orientation change your resources get re-allocated and re-created (because the Activity itself is destroyed).

There's some information about this on the Android Developer site, you could look at this specific topic here.

DigCamara
  • 5,540
  • 4
  • 36
  • 47
0

This is resolved using static inner Handler to deal with background processing of data.

The following code worked perfectly -

(Portrait Orientation)

static class BoardHandler extends Handler {
    private final WeakReference<BoardFragment> mService; 

    BoardHandler(BoardFragment service) {
    mService = new WeakReference<BoardFragment>(service);
    }

    @Override
    public void handleMessage(Message msg)
    {
    BoardFragment service = mService.get();
     if (service != null) {
          service.summaryData(msg);

          if(pDialog != null && pDialog.isShowing())
          pDialog.dismiss();
     }
    }
}

public void displayChart(final int maxDays) {

    BoardHandler myHandler = new BoardHandler(this);

    myHandler.sendEmptyMessage(maxDays);

}


 public void summaryData(Message msg)
 {
    //Data processing goes here.
 }

Note that how the use of WeakReference inside the inner Handler class helped to not only eliminate the memory leaks but also to show the progress dialog.

The Progess dialog show process is done while adding the fragment to backstack and doing commit in an activity whereas the dialog destroy is called after the summaryData method finishes its execution.

This may help someone who counts days on resolving similar issues.

sjain
  • 23,126
  • 28
  • 107
  • 185