OnAttach - null

When creating the fragment, I found that getActivity () is null. Therefore, in order to narrow down the problem, I saved a local copy of the activity in onAttach (Activity activity), which by definition is when it is attached to an activity.

However, I registered activity in onAttach and it is still zero.

I only encounter this problem in 2.3.6 and below.

Is this a known issue with the support package?

+7
source share
2 answers

A series of methods designed to bring the fragment to a renewed state:

  • onAttach (Activity), called as soon as the fragment is associated with its activity.
  • onCreate (Bundle) called to initially create the fragment.
  • onCreateView (LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
  • onActivityCreated (Bundle) tells the fragment that its activity has completed its own Activity.onCreate ().
  • onViewStateRestored (Bundle) tells the fragment that all the saved state of its view hierarchy has been restored.
  • onStart () makes the fragment visible to the user (depending on its launching activity).
  • onResume () makes the fragment interact with the user (based on its active activity, which resumes).

The bold method should be where getActivity no longer returns null.

The onAttach method should not be used to invoke methods on an activity object. It should be used to initialize callback interfaces. An example of these interfaces can be found here .

+7
source

This problem is related to the support package, it means that the fragment from Android 3.0 and higher is API level 11 and UP, so you will probably encounter the crash of the application for android 2.3.6 gingerbird

this.getActivity(); 
-3
source

All Articles