Use case:
The user start the app that will load a captcha. The user fill the captcha and try to download some information.
Problem:
If the user rotate the device while downloading the Activity
is destroyed. At the end of the execution the AsyncTask is trying to update the destroyed Activity
and the result is a "View not attached to window manager".
"Wrong" solution:
I can mask the problem with a android:configChanges="orientation|keyboardHidden|screenSize"
but with this the layout is not going to be updated to landscape.
What I'm asking:
Is it possible to change the orientation and change the "reference" of the Context passed to the AsyncTask
?
This answer suggest to check if the dialog is not null, but it's not what I'm looking for.
Here he suggests to use a WeakReference (here a nice snippet on how to use it) but I didn't understand if it's what I'm looking for.
To be more explicit this is what I'm doing in the onPostExecute:
@Override
protected void onPostExecute(Auto result) {
progress.dismiss();
new DownloaderCaptcha(context).execute("");
((EditText)context.findViewById(R.id.editTextCaptcha)).setText("");
context.findViewById(R.id.progrBar).setVisibility(View.VISIBLE);
context.findViewById(R.id.captcha).setVisibility(View.INVISIBLE);
if(result != null) {
Storage.storeHistory(context, result.getTarga().getValue());
Intent i = new Intent(context, MenuActivity.class);
i.putExtra("result", result);
context.startActivity(i);
} else {
ErrorDialog.show(context, error);
}
}