0

I have looked at many questions here and searched on Internet but couldn't find any specific solution. The main problem other questions refer to is, Window Leak Error when go from Portrait to Landscape or vice versa. And answer is mainly, because Activity is destroyed and recreated, hence Window Leak Error.

But my problem is, my device is already in Landscape, and app was working fine, previous Activities were smoothly running, but when I try to display ProgressDialog, Window Leak Error is thrown. Exact same code works fine when in Portrait orientation.

class DownloadYearChartAsyncTask extends AsyncTask<String, Integer, Boolean>
{
    private String username;
    private String city;

    private Resources resources;
    private Location locationOfCity = null;

    private ProgressDialog progressDialog;

    public DownloadYearChartAsyncTask(String username)
    {
        this.username = username;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        resources = getResources();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

        progressDialog = new ProgressDialog(CityActivity.this);
        progressDialog.setTitle(R.string.title_progress_dialog);
        progressDialog.setMessage(resources.getString(R.string.first_message_progress_dialog));
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        progressDialog.dismiss();

        boolean userUpdated = false;

        if(locationOfCity != null && city != null && !city.isEmpty()) {
            try
            {
                UserDAO userDAO = new UserDAO(CityActivity.this);
                User user = userDAO.readUser();

                if(user == null) {
                    if(username != null && !username.isEmpty()) {
                        user = new User(0, username, city, locationOfCity.getLatitude(), locationOfCity.getLongitude());
                        userUpdated = userDAO.createUser(user);
                    }
                }

                    if(username == null) {
                        user.setLocation(city);
                        user.setLatitude(locationOfCity.getLatitude());
                        user.setLongitude(locationOfCity.getLongitude());

                        userUpdated = userDAO.updateUser(user);
                    }
                }
                userDAO.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

        if(userUpdated)
        {
            Intent intent = new Intent(CityActivity.this, TodayActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        progressDialog.setProgress(values[0]);
    }

    @Override
    protected Boolean doInBackground(String... params) {
        boolean allDaysEntered = false;

        try {
            city = params[0];
            locationOfCity = MyJsonUtil.getLocationOfCity(params);

            List<Day> yearChart = MyJsonUtil.getYearChart(locationOfCity.getLatitude(), locationOfCity.getLongitude());


            if(yearChart != null && !yearChart.isEmpty()) {
                YearChartDAO yearChartDAO = new YearChartDAO(CityActivity.this);
                allDaysEntered = yearChartDAO.createYearChartWithDays(yearChart);
                yearChartDAO.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return allDaysEntered;
    }
}

Any suggestions or solutions will be appreciated.

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59

1 Answers1

0

The problem was with my code, where I used setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

Because, according to Kevin Gaudin's comment on this answer.

It looks like if you call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); when the device is not in its default orientation usage, then the activity orientation is immediately changed (destroyed and recreated) to the device default orientation. For example, on a phone, if you hold it in landscape orientation, then the activity is switched to portrait and back to landscape when reactivating sensors. The same opposite issue with an Archos A5 IT : using it in portrait causes the activity to be switched to landscape and back to portrait.

Here is the solution that I found, answered by CommonsWare. But this solution is a little lengthy.

So for people who are in a rush, this is the technique that I used but this solution is not perfect.

1 - In Manifest, add android:configChanges="orientation|screenSize" to your activity in which you want to show the ProgressDialog.

2 - In that Activity, just override OnConfigurationChange(Configuration newConfig):

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}
Community
  • 1
  • 1
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59