6

I am using fragments in my Android app. In these fragments I need a Context object for using it repeatedly (about 10 times) with some method calls.

As you know, I have 2 options:

OPTION I:

public class MyFragment extends Fragment{
    public void onCreate(Bundle savedInstanceState){
        super(savedInstanceState);

        //call a lot of methods and use getActivity() a lot of times
    }
}

OPTION II:

public class MyFragment extends Fragment{

    private Activity mActivity;

    public void onCreate(Bundle savedInstanceState){
        super(savedInstanceState);

        mActivity = getActivity();
        //call a lot of methods and use mActivity
    }
}

I know that declaring mActivity field will require some memory ( how much? ), but I think calling getActivity will need some CPU processing.

Which of these 2 options is better and why?


EDIT:

Well, looking the Android source code I can find the source of getActivity() method inside Fragment class:

final public FragmentActivity getActivity() {
    return mActivity;
}

So, as you can see, in the Option II mActivity is reserved twice, which is a waste of memory so from now I will use Option I.

Thanks for your answers, they made me understand about this :)

BamsBamx
  • 4,139
  • 4
  • 38
  • 63
  • 1
    Declaring the field will cost you all of...i think 8-12 bytes. And save you from having to call `getActivity()` every time. I'm not sure how reliable it'd be if the app were unpickled, though. (Hopefully everything would be recreated properly...but eh. I don't know Android.) If you just want to use the activity within `onCreate`, i'd make the variable a local. – cHao Dec 17 '13 at 23:40
  • in terms of performance, there's not that much difference really.. so unless you're really trying to cut down on memory/cpu as much as possible, just call getActivity(). it's a little bit more readable. – peter Dec 17 '13 at 23:52

3 Answers3

4

but I think calling getActivity will need some CPU processing.

It is less than you think. getActivity is just acessing a field, getting the Activity. it doesn't have much CPU involved. Your second method which will require a little heap of memory and first will require a little stack, see this for performance.

In the other hand. This is premature optimization. if you don't have any memory problems when current code.

Community
  • 1
  • 1
wtsang02
  • 18,603
  • 10
  • 49
  • 67
3

Well, looking the Android source code I can find the source of getActivity() method inside Fragment class:

final public FragmentActivity getActivity() {
    return mActivity;
}

So, as you can see, in the Option II mActivity is reserved twice, which is a waste of memory so from now I will use Option I.

Thanks for your answers, they made me understand which about this :)

BamsBamx
  • 4,139
  • 4
  • 38
  • 63
  • mActivity isn't reserved twice. When you set in optionII `mActivity = getActivity();` you are only setting the reference. Your activity instance only exsit 1 place in the memory, and `mActivity` is simply pointing to that place in memory. It wounldn't even make sense if you have two instance of your activity. – wtsang02 Jan 15 '14 at 17:49
2

The second option is better. You will only use getActivity() once. In the first option you will call it many times which is expensive. Declaring mActivity will ofcource cost some memory. Since mActivity is not really a object, but a reference to the object, it won't take that much memory.

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116