Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6719311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:04:40+00:00 2026-05-26T09:04:40+00:00

In a Fragment should you call getActivity() each time you need a reference to

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:04:41+00:00Added an answer on May 26, 2026 at 9:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With fragment, it seems that, I can use one activity with many fragments to
I use a fragment only inside one specific parent activity. Now I wonder if
For the following code fragment. /*This program demonstartes how a virtual table pointer *
so this is a fragment of a procedure that exports a dataset from access
Im not sure about what authentification method I should use for my webservice. I've
Here's a fragment of code I'm prototyping that should, by all accounts, never see
Every time I need to do something N times inside an algorithm using C#
This is more of a 'How should I?' rather than 'How do I?' question.
Given the following XML fragment: <foo> <bar>1</bar> <bar>2</bar> <bar>3</bar> </foo> The following XSL should
In this code fragment: $results = $this->getAdapter()->fetchAll($query); if(count($results)) { // … } …do you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.