[Solved] How insert Floating Action Button inside extends fragment


fab_twitte=(FloatingActionButton)findViewById(fab_twitte); 

you setting fab_twitte here ,but adding a listener with fab_twi.setOnClickListener where did fab_twi should be fab_twitte.

the main problem is the way you are creating the views for the fragment. I sure you read about the life cycle of fragments where the fragments are attach and detach from their corresponding activity.

Do not inflate your layout in onCreate, use onViewCreated becuase this is the equivalent for the activity onCreate. Use the onCreate in fragments to initialize stuff not bind your layout.

Here is a more detail explanation.Android fragments have their own life cycle very similar to an android activity. This section briefs different stages of its life cycle.
onAttach()The fragment instance is associated with an activity instance.The fragment and the activity is not fully initialized. Typically you get in this method a reference to the activity which uses the fragment for further initialization work.

onCreate() The system calls this method when creating the fragment. You should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

onCreateView() The system calls this callback when it’s time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component from this method that is the root of your fragment’s layout. You can return null if the fragment does not provide a UI.

onActivityCreated()The onActivityCreated() is called after the onCreateView() method when the host activity is created. Activity and fragment instance have been created as well as the view hierarchy of the activity. At this point, view can be accessed with the findViewById() method. example. In this method you can instantiate objects which require a Context object

onStart()The onStart() method is called once the fragment gets visible.

onResume()Fragment becomes active.

onPause() The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.

onStop()Fragment going to be stopped by calling onStop()

onDestroyView()Fragment view will destroy after call this method

onDestroy()onDestroy() called to do final clean up of the fragment’s state but Not guaranteed to be called by the Android platform.

so just change from onCreate to onViewCreated ,plus fix the changes i stated in the aforementioned.

**** also do not use getApplicationContext, use getActivity() this returns the context for the current activity it is attached to.In simple terms getApplicationContext returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity. So no do not use it.

10

solved How insert Floating Action Button inside extends fragment