2

I am planning to convert the activities in my app to fragments to implement a drawer. But I have never used fragments before and they seem kinda complicated, so I'm asking for some quick help:

What are the basic "rules" to convert from activity to fragment?

All I have so far is that

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

becomes

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_main, container, false);

What else? I guess I also have to change each layout from Linear/Relative to Fragment.

EDIT I can't seem to figure it out properly. I have 3 activities:

A - base activity, no layout, starts service, threads, does a lot. Most back-end

B - extends A, has a layout. It's the main startup activity. Most front-end

C - extends A, has a layout. It's started via button from B.

Any suggestions on which and how to turn them to fragments?

KKO
  • 1,913
  • 3
  • 27
  • 35
  • check out this basic project setup I laid out, I think it makes seeing how Fragments work fairly clear: http://codereview.stackexchange.com/questions/57543/why-does-the-new-adt-create-a-static-inner-class-fragment-by-default – EpicPandaForce Aug 19 '14 at 18:22

2 Answers2

1

Have your fragment inherit from Fragment or an appropriate subclass.

Make sure your main activity has a DrawerLayout in the XML. You can alternatively use

getFragmentManager().beginTransaction.Add

to add your drawer to the view.

Extra Reference:
http://developer.android.com/training/implementing-navigation/nav-drawer.html http://developer.android.com/guide/components/fragments.html

John Li
  • 46
  • 3
  • I have already implemented the drawer in the main activity. My problem is converting the other activities into fragments to add them here – KKO Aug 19 '14 at 13:34
  • There should be minimal effort in converting activities to fragments because they are very similar. Do you have a specific problem that I might be able to address? – John Li Aug 19 '14 at 14:12
  • Some simple instructions on how to convert, like "this turns to that", because I have absolutely no experience with Fragments so I don't know what I should change, what I should delete and what I should add instead – KKO Aug 19 '14 at 14:37
  • My advice for you is to just go ahead and convert it the best you can. Don't let your inexperience roadblock you from experimenting and pioneering. There are very little changes between the two. The main difference would be how you add it in your app whether it is through XML or through code. If you run into a problem you can't solve, let me know and I'll help you out. – John Li Aug 19 '14 at 15:06
  • when I change from extends Activity to extends Fragment, everything goes haywire. I have to put getActivity() everywhere right? (and replace all "this" with getActivity()) – KKO Aug 19 '14 at 17:36
  • Store the Activity as a class variable instead of calling it multiple times but that is the way to go. – John Li Aug 19 '14 at 17:45
  • please see my edit, cause I'm really lost. How should I go about it? – KKO Aug 19 '14 at 17:58
0

Okay, let's keep it simple then. Let's say I have ListFragment that I want to put in my activity. One way I would add it would be to:

  • add a frame_layout to the xml and with the layout settings and id it "@id+/example_list"
  • in onCreate of the activity, add the code

    CustomListFragmentClass myCustomListFragmentClass;
    getFragmentManager().beginTransaction()
    .replace(R.id.example_list, myCustomListFragmentClass, "listFrag").commit();
    

The fragment should now be visible.

John Li
  • 46
  • 3
  • I have no idea what this means :) Is this related to my edit? Cause those 3 classes are not hypothetical, I really have them like that and I don't know how and which one to turn to fragment – KKO Aug 19 '14 at 18:56