14

I'm having issues finding a working example of using fragments + RoboGuice. The problem happens when you attempt to add/remove fragments with the Android fragment transaction manager. Once you tell the fragment to inherit from RoboFragment the transaction manager no longer thinks the class is a fragment (because it extends RoboFragment). You can however use RoboGuice's own fragment manager but it also crashes. Is there any examples out there of adding/removing RoboGuice fragments dynamically?

danb
  • 10,239
  • 14
  • 60
  • 76
technoviking
  • 161
  • 3
  • 7
  • I have RoboGuice and Fragments working in an app nicely. Can you provide the stack trace so we can see what the issue is? – Donn Felker Nov 29 '11 at 17:36
  • Also, what version of the compatibility library are you using? – Donn Felker Nov 29 '11 at 17:38
  • I am looking for some examples as well, I seem to get lots of NPE's when trying to wire up InjectFragment stuff.. I'd love to see some working examples. – danb Dec 12 '11 at 16:56

2 Answers2

13

I've recently started using fragments on a new project, and the following is the code I'm using

I'm not inheriting from the RoboFragment class, but I'm doing exactly the same by adding the following lines to my onCreate and onViewCreated methods. Inheriting from RoboFragment shouldn't behave any different, in fact this is what RoboFragment looks like.

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RoboGuice.getInjector(getActivity()).injectMembersWithoutViews(this);
}

public void onViewCreated(final View view, final Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    RoboGuice.getInjector(getActivity()).injectViewMembers(this);
    // Do whatever with your injected views.
}

Obviously I've also implemented onCreateView.

Then, in my Activity I extend FragmentActivity as I'm using the compatibility package. Note, you must use FragmentActivity if your wanting compatibility with pre API level 11. If you're just supporting 11 plus you don't need the compatibility lib or to use FragementActivity. In my activity I'm then using the following to add the fragment to my page.

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragmentHolder, new MyFragment());
transaction.commit();

The type of R.id.fragmentHolder is a FrameLayout.

Everything works fine with this and I'm able to use all my injected resources and views in my fragment. For completeness I'm using the latest 2.0-SNAPSHOT of roboguice with version r6 of the compatibity-v4 library against Android 2.2.1.

Kingamajick
  • 2,281
  • 1
  • 16
  • 19
  • Thanks King. Do you have any code you have found or can make available of fragments actually doing meaningful things? – danb Jan 20 '12 at 16:01
  • 1
    I'm afraid I don't, however you should be able to do anything you can do with an Activity in a Fragment. The only different part is passing data from one Fragment to another as they shouldn't communicate directly. The way I've seen this approach this is to check the type of getActivity() in onCreate() and make sure its of some interface you've created to accept data from the fragment. This way your Activity can implement this interface and pass the data if needed to the next fragment. – Kingamajick Jan 21 '12 at 13:10
  • Are you sure this works? When I try it with Roboguice 2.0 I get `IllegalArgumentException: MyFragmentActivity does not appear to be a RoboGuice context (instanceof RoboContext)` – Jeff Axelrod Sep 24 '12 at 19:08
  • Is your Activity a RoboFragmentActivity? – Kingamajick Sep 25 '12 at 13:12
0

Roboguice 1.x is not compatible with the compat library and fragments. You will either have to move 2.0 which is in beta or extend the Fragment* classes yourselves.

More information is available at:

http://groups.google.com/group/roboguice/browse_thread/thread/2858bc10b83b6beb

Arif Amirani
  • 26,265
  • 3
  • 33
  • 30