When a fragment gets created, it goes through the following state.
[code lang=”js”]
oncreate()
onAttache()
onCreate()
onCreateView()
onActivityCreated()
[/code]
[code lang=”js”]
onStart()
onResume()
[/code]
[code lang=”js”]
onPause()
onStop()
[/code]
[code lang=”js”]
onDestroyView()
onDestroy()
onDetach()
[/code]
[code lang=”js”]
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, new fragment(position ))
.commit();
[/code]
Right click on src/ package-New-Fragment-Fragment(Blank)
[code lang=”js”]
Fragment_fragment_one_xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mobilemerit.com.fragmentexample.FragmentOne">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/hello_blank_fragment_one" />
</FrameLayout>
[/code]
[code lang=”js”]
public class FragmentOne extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
// TODO: Rename and change types and number of parameters
public static FragmentOne newInstance(int position) {
FragmentOne fragment = new FragmentOne();
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, position);
fragment.setArguments(args);
return fragment;
}
public FragmentOne() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached
(getArguments().getInt(ARG_PARAM1));
}
}
[/code]
Tags: Fragment in Android
Warning: _() expects exactly 1 parameter, 2 given in /home2/mobilemeri7/public_html/wp-content/themes/mobilemerit/comments.php on line 28