Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onAttach() not called in Fragment

My Fragment doesn't call onAttach(context) method when it launched from AppCompatActivity.

Fragment creating in XML:

<fragment     android:id="@+id/toolbar"     class="package.MainToolbarFragment"     android:layout_width="match_parent"     android:layout_height="wrap_content"     tools:layout="@layout/fragment_main_toolbar" /> 

But if I extends it from support.v4.Fragment, onAttach(context) call !

What could be the problem?

Of course, I can extend all fragments from v4.Fragment, but I don't want it. Is it bad practice? Also project min sdk 14.

like image 831
winston Avatar asked Sep 16 '15 09:09

winston


People also ask

What is onAttach in fragment?

onAttach(Activity) called once the fragment is associated with its activity. onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.

Is onAttach called before onCreateView?

Bookmark this question. Show activity on this post. In a Fragment's Lifecycle, the onAttach() method is called before the onCreate() method.

Can a fragment exist without an activity?

It can't exist independently. We can't create multi-screen UI without using fragment in an activity, After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity.

What is the Life cycle of android fragment?

Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity. Each fragment has its own life cycle methods that is affected by activity life cycle because fragments are embedded in activity. The FragmentManager class is responsible to make interaction between fragment objects.


2 Answers

It's not called because this method has been added in API 23. If you run your application on a device with API 23 (marshmallow) then onAttach(Context) will be called. On all previous Android Versions onAttach(Activity) will be called.

http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity)

Support libraries fragment is platform independent. Hence it works on all API versions.

like image 77
sockeqwe Avatar answered Sep 19 '22 14:09

sockeqwe


While Google wants us to stop using deprecated API's

@Override public void onAttach(Context context) {     super.onAttach(context);     ... 

Is so new that it isn't widely called. You need to also implement

@Override public void onAttach(Activity activity) {     super.onAttach(activity);     ... 

For me they are identical but I like to KISS and introducing another support library tends to double my apk to about 1000kb. I only updated my SDK yesterday.

The reason the types are not interchangeable here, as they are in many instances, is that the method taking an Activity will still be called when an Activity is provided as they are both publicly visible and Activity is more specialised than (as a sub class of) Context so will take precedence.

like image 41
John Avatar answered Sep 16 '22 14:09

John