3

I'm dealing with fragments.
I have an Activity and different fragments.
Each fragment need the access to a Class(call it X) that allow it to access a database, but, because I have a lot of fragments, I don't want to create a different instance of the Class X in every fragment as I think it will require lots of memory.
So how can I do?
I wrote something like this (with a getter), but it doesn't work!

public class MyActivity {
  private ClassX classx;

  .....

  public ClassX getClassX() {
     return classx;
  }   
  .....
}

But than, how can I call it from the fragment?

Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
user1315621
  • 3,044
  • 9
  • 42
  • 86

4 Answers4

12

From the fragment call your activity's method

((MyActivity )  getActivity()).getClassX() ;
Atul Bhardwaj
  • 6,647
  • 5
  • 45
  • 63
3

This is a little bit more of a Java question and android.

If you looking at accessing the database, look at creating a database singleton.

So something like:

public class Database {

    // This starts off null
    private static Database mInstance;

    /**
     * Singleton method, will return the same object each time.
     */
    public static final Database getInstance() {
        // First time this method is called by Database.getInstance() from anywhere
        // in your App. It will create this Object once.
        if(mInstance == null) mInstance = new Database();
        // Returns the created object from a statically assigned field so its never
        // destroyed until you do it manually.
        return mInstance;
    }

    //Private constructor to stop you from creating this object by accident
    private Database(){
      //Init db object
    }

}

So then from your fragments and activities you can then place the following field in your class's (Better use use a base activity and fragment to save you repeating code).

public abstract class BaseFragment extends Fragment {

    protected final Database mDatabase = Database.getInstance();

}

Then your concrete fragments can extend your BaseFragment e.g. SearchListFragment extends BaseFragment

Hope this helps.

Worth reading about singletons and database

Regards, Chris

Chris.Jenkins
  • 13,051
  • 4
  • 60
  • 61
  • I'm immediately starting to read about singleton. Never heard about but sounds really interesting! So if I use it, I will never need to initialize the singleton class with classic way "MyObject a = new MyObject()" ? – user1315621 Aug 22 '12 at 09:52
  • The initialisation is done in the Singleton class thus calling the class statically: `Database.getInstance()` an example in the Android/Java framework would be: `Calendar.getInstance()` That creates the `Calendar` object for you. – Chris.Jenkins Aug 22 '12 at 10:11
  • 1
    It's useful methodology, but only use it for Objects that you are _always_ going to need/use, as the object is strongly referenced statically, so it is almost never going to be GC'd. So Database/Application/Prefs etc. Avoid for Model data! – Chris.Jenkins Aug 22 '12 at 10:15
  • Wow, that's fantastic! So, just to be sure... I can call Database class from every part of my code using Database.getInstance() and the code will always refer to same instance? And what happens if the object goes to GC's? It will be just slow or it will make some errors? – user1315621 Aug 22 '12 at 10:22
  • Correct. But just to _make_ you aware its not 100% as with any Object in Java it is up for GC, but thats not to scare you, just to increase your understanding. Either-way, worse case if it does get GC'd between lifecyle at least your Activity/Fragment's etc are using the same Object - But I have never come across that issue before :) – Chris.Jenkins Aug 22 '12 at 10:29
2

Define an interface called Callbacks (or something else if you want). In it, have a public method called getClassX(). Then make your Activity implement the Callbacks interface.

In your Fragments, in onAttach, store a reference to a Callbacks object (i.e. your activity via something like:

if(activity instanceof Callbacks)
    mCallbacks = (Callbacks)activity;

This will guarantee that the Fragments are able to call the function. (in case you want to reuse the fragments later in another app)

Then in your Activity, in onCreate(), create an instance of ClassX. In your getClassX() method, just return a reference to it.

When you want a reference to it from your Fragments, call mCallbacks.getClassX() and you should be sorted.

Mike T
  • 4,747
  • 4
  • 32
  • 52
  • This is the right approach. You can read more here: `http://developer.android.com/training/basics/fragments/communicating.html` – HGPB Mar 21 '13 at 19:17
0

You can use a static object in your activity, and use it from the fragment, or call the getActivity() method in your fragment to access the whole activity objects/methods

Aerilys
  • 1,628
  • 1
  • 16
  • 22