This call, which occurs in a Fragment, occasionally crashes due to a NullPointerException, especially when the app is not running in the foreground:
getActivity().getApplication());
This call occurs when feedback comes back from the server or when there's a need to redraw the fragment. I'm not sure why that call would throw a NPE, can the fragment remain in memory while the Activity gets GCed?
If it makes a difference, I'm using a SwipeyTab ViewPager to display different fragments.
We have three ways to achieve same - 1) Increasing the Heap -Eden space size . 2) Create Singleton class with Static reference . 3) Override finalize() method and never let that object dereference. Save this answer.
A Fragment is not an Activity. Fragments are hosted inside a FragmentActivity.
The garbage collector will free the memory after you "destroy" the reference. i. 3 Setting the object reference to null.
A paused Fragment is still alive (all state and member information is retained by the system), but it will be destroyed if the Activity is destroyed. If the user presses the Back button and the Fragment is returned from the back stack, the lifecycle resumes with the onCreateView() callback.
Fragment
s can't exist without an attached Activity
. If the activity is destroyed, then so will the fragment. Also note that getActivity()
will return null
until onAttach()
is called on the fragment.
The issue was there was a long running thread on the Fragment, which returned results after the Fragment was deattached from the Activity. It seems strange for the Fragment to exist without being attached to an Activity (after it was already attached).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With