App Crashes On Asynctask Screen Rotation
I'm using AsyncTask in one of the fragments of an activity. The AsyncTask is working perfectly but on screen rotation, it loses the reference to activity and the variable returns
Solution 1:
There are plenty of solutions to this. One simple one for you is to use what's known as a "Retain" fragment. This is an approach that is used in some of the Google samples. A retain fragment has no UI, and persists across orientation change. This is usually defined as a static inner class of your activity like so:
publicstaticclassRetainFragmentextendsFragment {
privatestaticfinalStringTAG="RetainFragment";
publicRetainFragment() {}
publicstatic RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
RetainFragmentfragment= (RetainFragment) fm.findFragmentByTag(TAG);
if (fragment == null) {
fragment = newRetainFragment();
}
return fragment;
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
}
Then, put your AsyncTask inside of your RetainFragment. When onPostExecute
is called, you can simply use the fragment's getActivity()
method to get the Activity that it is attached to.
Hope that helps, let me know if there is still some confusion!
Edit:Here is a Google sample of a retain fragment
Edit 2: (as per comments)
publicclassA{
// This class holds a reference to it's outer A instance. It can be// accessed using A.this.publicclassinnerClassA{
//...
}
// This class does not hold a reference to it's outer A instance.publicstaticclassinnerClassB{
//...
}
}
Edit 3: In the comments, I ended up writing the full code anyway. For anyone interested:
publicclassMyActivityextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button b = (Button) findViewById(R.id.aaa);
b.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
RetainFragment retainFragment =
RetainFragment.findOrCreateRetainFragment(getFragmentManager());
retainFragment.loadAsync();
}
});
}
privatevoidhandleResponse(String response) {
// do something with the response...
}
publicstaticclassRetainFragmentextendsFragment {
privatestatic final StringTAG = "RetainFragment";
publicRetainFragment() {
}
publicstaticRetainFragmentfindOrCreateRetainFragment(FragmentManager fm) {
RetainFragment fragment = (RetainFragment) fm.findFragmentByTag(TAG);
if (fragment == null) {
fragment = newRetainFragment();
fm.beginTransaction().add(fragment, TAG).commit();
}
return fragment;
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
privatevoidloadAsync() {
newAsyncTask<Void, Void, String>() {
@OverrideprotectedStringdoInBackground(Void... params) {
// Do some work...returnnull;
}
@OverrideprotectedvoidonPostExecute(String response) {
MyActivity myActivity = (MyActivity)getActivity();
if (myActivity != null) {
myActivity.handleResponse(response);
}
}
}.execute();
}
}
}
Solution 2:
Try this way
<activityandroid:name=".ActivityName"android:configChanges="orientation|screenSize|keyboardHidden"/>
Post a Comment for "App Crashes On Asynctask Screen Rotation"