android - AsyncTask, Fragments, Views, and Backstacks -


I am using this method for a while. Here is a very useful example of an asyncask + progress indicator:

  new asyncTask & lt; Zero, zero, zero> () {WeakReference & lt; MyFragment & gt; Weak paragraphs = new week reference & lt; MyFragment & gt; (MyFragment.this); @ OverEride Protected Zero at PreExecute () {piece slice = weakFragment.get (); If (piece! = Null) fragment.getView () FindViewById (RID.progress) .setVisibility (see .Visual); } @ Override protected document doInBackground (zero ... parameter) {fragment piece = weakfragment.gate (); If (fragment == zero) return zero; // something ...} @Override protected void onPostExecute () {piece slice = weakFragment.get (); If (piece! = Null) fragment.getView () FindViewById (RID. Progress) .setVisibility (see GONE); }}. Execute ();  

It works well with orientation changes, but I have found that this piece is non-empty and fragmented. GetView () when I'm snapped, when I pop the piece from the backstack, it is obviously due to the accident, what are you using the approach? I can not find the right solution online online Important note, this is in one piece and I call setRetainInstance (true); its reactivated (...) on that piece.

asynchronous is an incomprehensible task which means that you usually do not Know when it's complete and when it calls onPostExecute () . I think your problem is here, when you rotate your device, your acnecutcus has not yet completed and your piece has been destroyed and again it is ready again very quickly, while still acetankaskar Thread is on and when it has finished its work, then there is a return value of getView in your piece and not null but if the time of rotation is too high thenYou will get null because the async task has ended but there is no view in your piece. Now this scenario is okay for your backstack when you place your piece on your backstack Push, its view is destroyed (), but not the fragment (Returns the piece in the layout from the backstack). Now your asyncTask ends and call your section getView method and it does not have any view So you will get NPE .

My solution:

First you WeakReference & lt; ProgressBar & gt; instead of WeakReference & lt; MyFragment & gt; / Code> and onPostExecute () Check if it's empty or not, if it's zero, then nothing is because the default value is invisible if this is not an empty call then setVisibility .GONE).

Update:

This piece is alive but the view has been dealloc'd. Is this possible?

Yes, just look at figure2 from the link when the piece is activated (green box) added piece for the back stack section on , onStop onDestroyView . Then when this piece goes back to the heap, it is correct on the onCreateView arrow link with this text: Returns the piece from the backstacks .

To confirm my answer, you can create AsyncTask and call SystemClock.sleep (30000) your doInbackground < / Code>. Now you can keep your piece on the backstack and pop it up with backstacks without exception, because the aciccux is not finished and when it calls onPostExecute () first There is only one view from

You can see that there is another good thing

It can only be used with pieces that are not in the back stack

So when you press your piece back into the heap If you have may be completely destroyed and you can be it get a new piece reference, but when you use setRetainInstance (true) Then your piece for the configuration changes the entire activity is rebuilt which means that it is a similar piece and onCreate is not called and it is very important because if you type ascyntask on onCreate Start the law, when you press your piece back into the heap and pop it up, there can be a new piece of and it means the code runs on the onCreate And another is removed as AsyncTask . This means that you have no control over the number of calling, AsyncTask s, so look for memory leaks!


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -