Firstly, I would recommend reading this Fragments . Pay particular attention to the section of the created fragment that includes the fragment life cycle diagram. The second download and compilation of this sample application , an effective navigation application will help you understand how different fragments work in tandem and even implements an action bar.
To answer your question more or less, a fragment can be considered as a separate class. After calling this particular fragment, you can call functions from this class.
Fragment switch switch
This is a sample code to show you what I mean.
public Fragment getItem(int i){ switch (i) { case 0: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return new LaunchpadSectionFragment(); case 1: return new BluetoothClass(); default: // The GPS section of the app . Fragment fragment = new DummySectionFragment(); Bundle args = new Bundle(); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); fragment.setArguments(args); return fragment; } }
In this case, each fragment for me represented a class that was implemented on a separate tab, and each tab had its own functionality. One of the key benefits of snippets is that you can run individual actions without first completing one action.
In addition, each fragment is an extension of the java.lang.Object library. Thus, it has all these features + additional. I would read this one . Finally, it would be nice to have separate XML files for each fragment, then you can display them separately when calling the fragment.
One more code
Each fragment will / may have this
public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); // Do stuff on creation. This is usually where you add the bulk of your code. Like clickListners View rootview = inflater.inflate(R.layout.xml_the_fragment_uses container,false); rootview.findViewById(R.id.your_id).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Do something } }); } public void onStart(){ super.onStart(); Toast.makeText(getActivity(), "Fragment started",Toast.LENGTH_SHORT).show(); } public void onResume(){ super.onStart(); Toast.makeText(getActivity(), "Fragment Resumed",Toast.LENGTH_SHORT).show(); } public void onStop(){ super.onStart(); Toast.makeText(getActivity(), "Fragment Stoped",Toast.LENGTH_SHORT).show(); disableBT(); }
Remember that these functions are taken from the fragment life cycle that I mentioned earlier.
Hope this gave you some idea of ββthe fragments. Also, don't forget to read this , as many features use the v7 application compatibility library. Including fragment manager .