Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android & OOP - Global Variables vs Inherited Getters in Fragments

In a Fragment should you call getActivity() each time you need a reference to the activity or create a global variable 'mActivity' and use this.

Basically you already have an Activity object (getActivity()) and it feels like creating a global (mActivity) is code duplication and creating an extra reference that is unneeded. But also using getActivity() everywhere looks horrid and feels wrong doing multiple method call's each time (performance?).

 // Pseudo Android
 public class MyFragent extends Fragment {

      private Activity mActivity; // Global

      public void onActivityCreated(Bundle b){

          mActivity = getActivity();

      }

      public void onClick(View v){

         randomMethodTakingActivity(mActivity);
         // or
         randomMethodTakingActivity(getActivity());

      }

      private void someMethod(){
         randomMethodTakingActivity(mActivity);
         // or
         randomMethodTakingActivity(getActivity());
      }

      private void anotherMethod(){
         mActivity.someCallback();
         // or
         getActivity().someCallback();
      }

 }

This would also be relevant for getApplication() or getView();

I've read through Coding for Performance but can't see anything relevant. I'd like some feedback on the OO nature and also performance (though probably negligible).

like image 349
Blundell Avatar asked Oct 26 '11 09:10

Blundell


People also ask

Is Android good or Apple?

Apple and Google both have fantastic app stores. But Android is far superior at organizing apps, letting you put important stuff on the home screens and hide less useful apps in the app drawer. Also, Android's widgets are much more useful than Apple's.

What's meant by Android?

Android OS is a Linux-based mobile operating system that primarily runs on smartphones and tablets. The Android platform includes an operating system based upon the Linux kernel, a GUI, a web browser and end-user applications that can be downloaded.

What is Android latest operating system?

The latest version of Android OS is 12, released in October 2021. Learn more about OS 12, including its key features. Older versions of Android include: Tiramisu (OS 13)

Who is the owner of Android?

Android Inc., was bought by the American search engine company Google Inc., in 2005. At Google, the Android team decided to base their project on Linux, an open source operating system for personal computers.


1 Answers

In a Fragment should you call getActivity() each time you need a reference to the activity or create a global variable 'mActivity' and use this.

Call getActivity(). For starters, if you are using setRetainInstance(true), your alternative approach is simply wrong -- you will be pointing to the wrong Activity after a configuration change.

it feels like creating a global (mActivity) is code duplication and creating an extra reference that is unneeded

Absolutely.

using getActivity() everywhere looks horrid

You are certainly welcome to your opinion.

feels wrong doing multiple method call's each time (performance?)

If you will be calling it a million times in a tight loop, create a local variable for a temporary cache. Otherwise, the performance hit should not be material.

Or, another way of looking at it, if Traceview says you're spending too much time calling getActivity(), then worry about it.

like image 76
CommonsWare Avatar answered Sep 27 '22 17:09

CommonsWare