Activity
. Interaction with fragments
is done through FragmentManager
, which can be obtained via
Activity.getFragmentManager()
and
Fragment.getFragmentManager()
.he core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:
-
onCreate(Bundle)
called to do initial creation of 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 ownActivity.onCreate()
.
onCreate():
The
In this method, you can assign variables, get
defined syntax:
This is called after
onCreateView():
After the
defined syntax:
This will be called between
onActivityCreated():
As the name states, this is called after the
defined syntax:
This is called after
This information taken from android official website: developer.android.com
Someone ask and Farbod reply nicely on stackoverflow
and beutiful explanation of fragment and its use in Newcircle.
Happy Coding!!!
The
onCreate()
method in a Fragment
is called after the Activity
's onAttachFragment()
but before that Fragment
's onCreateView()
.In this method, you can assign variables, get
Intent
extras, and anything else that doesn't involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be called when the Activity
's onCreate()
is not finished, and so trying to access the View hierarchy here may result in a crash.defined syntax:
void onCreate (Bundle savedInstanceState)Here is the some code, how to declare:
public void onCreate(Bundle savedInstanceState) { // ... if (savedInstanceState == null) { FragmentManager fragmentManager = getFragmentManager() // Or: FragmentManager fragmentManager = getSupportFragmentManager() FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit(); } }
onAttach(Activity)
and before
onCreateView(LayoutInflater, ViewGroup, Bundle)
, but is not called if the fragment
instance is retained across Activity re-creation (see setRetainInstance(boolean)
). onCreateView():
After the
onCreate()
is called (in the Fragment
), the Fragment
's onCreateView()
is called. You can assign your View
variables and do any graphical initialisations. You are expected to return a View
from this method, and this is the main UI view, but if your Fragment
does not use any layouts or graphics, you can return null
(happens by default if you don't override).defined syntax:
View onCreateView (LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)Here is the code, how to declare:
public class FirstFragment extends Fragment implements OnClickListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.first_fragment, container, false); Button nextButton = (Button) view.findViewById(R.id.button_first); nextButton.setOnClickListener(this); return view; } // ... }
This will be called between
onCreate(Bundle)
and onActivityCreated(Bundle)
onActivityCreated():
As the name states, this is called after the
Activity
's onCreate()
has completed. It is called after onCreateView()
, and is mainly used for final initialisations (for example, modifying UI elements).If your view is static, then moving any code to the onActivityCreated
method is not necessary. But when you - for instance, fill some lists
from the adapter, then you should do it in the onActivityCreated method
as well as restoring the view state when setRetainInstance used to do
so. defined syntax:
void onActivityCreated (Bundle savedInstanceState)Here is the come code how to declare:
public class TestFragment extends Fragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(WebInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.web.fragment_web, container, false); } }
onCreateView(LayoutInflater, ViewGroup, Bundle)
and before onViewStateRestored(Bundle)
.This information taken from android official website: developer.android.com
Someone ask and Farbod reply nicely on stackoverflow
and beutiful explanation of fragment and its use in Newcircle.
Happy Coding!!!