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.