I use fragments in my android app. In these fragments, I need a Context object to reuse it (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);
OPTION II:
public class MyFragment extends Fragment{ private Activity mActivity; public void onCreate(Bundle savedInstanceState){ super(savedInstanceState); mActivity = getActivity();
I know that declaring the mActivity field will require some memory (how much?), But I think that calling processor getActivity will require some processing.
Which of these two options is better and why?
EDIT:
Well, looking at the Android source code, I can find the source of the getActivity () method inside the Fragment class:
final public FragmentActivity getActivity() { return mActivity; }
So, as you can see, in Option II, mActivity is reserved twice, which is a waste of memory, so now I will use Option I.
Thanks for your answers, they made me understand about it :)
source share