0

When using fragments, am I supposed to have one activity and all I do in my app is changing fragments? Or is it that I should see fragments as div's in HTML and have for instance the heading a fragment and the body a fragment and for more complex things such as database-queries I use a new activity?

Philip
  • 1,068
  • 3
  • 12
  • 21

2 Answers2

2

Fragments offer the concept of reusable sections of code. You can have an activity associated several fragments or have one fragment tied to multiple activities. You can have the heading as a fragment and the body of a fragment if that suits your purposes - the heading remains the same but the page changes or vice versa. Basically the power of fragments comes in that it provides efficient code reuse.

Your fragments can be just as complex as your activities. To get to the context referring to the fragment, all you have to do is call the getActivity() method and you can perform any and every action you want. You can execute database queries in your fragments in any manner you want. You can have create background tasks using AsyncTask, execute queries within AsyncTasks, create and inflate UIs, create menus, get access to menu items.... you get the general idea.

But with this said, one thing to keep in mind is that some areas of Android are more elegantly handled by Activities - like creating and registering receivers, services, push notification, etc. and for those I would recommend that you stick with Activities but it really is just my opinion and you might find a more elegant solution using fragments if it suits your purposes.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
1

A good idea is to create seperate classes for each fragment. Each class and therefore each fragment will serve a particular task or objective. It is also a good rule of thumb to separate your xml's for each fragment. This especially comes in handy when you are also implementing an action bar. There is no restriction most times between sharing information between fragments as well. So for example if you want to pass items in a list view (in one fragment) and then display them in another there is nothing stoping you.

Remember each fragment has its own lifecycle (onCreate, onCreateView, onResume etc etc ) these lifecycle events are the key to managing each individual fragment. Please read this page I have explained quite in detail what fragments are.

One last thing it might be advisable to know what message handlers and asynch tasks are.These two utilities are the foundations upon which most threading & UI is effectively achieved.

Update

One more thing pay particular attention to fragment managers. Note what kind you are using, one destroy's all instances when a fragment is closed while the other retains. This is done to save memory if your app is memory intensive choose wisely.

Comments Answers

yes you need fragmentManager().

You declare a fragments as follows.

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

You declare a fragment manager as follows.

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it.

For example:

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

Do not worry about Activity vs ActionBarActivity. ActionBarActivity is an extension of Activity() and is backward compatible.

Good Luck

Community
  • 1
  • 1
SeahawksRdaBest
  • 868
  • 5
  • 17
  • Thanks! One more question, to use fragments i need the fragmentManager(), right? I get it by extending the Fragments-Class. Since downloading the KitKat-SDK the IDE automatically extends my Activity with ActionBarActivity though. How do I properly implement the Fragment-Class? – Philip Mar 15 '14 at 01:48