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 9045621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:23:33+00:00 2026-06-16T11:23:33+00:00

Should I use _activity = this; ? I’ve seen _activity referenced many times in

  • 0

Should I use “_activity = this;“?

I’ve seen _activity referenced many times in sample code. So, I arbitrarily decided that it looked like a good practice and have been using in all my code for awhile (over a year). But, before I start spreading the word around more I wanted to find some proper documentation that using a global (activity-local) context variable is good practice or not.

Anybody have ideas/thoughts/links? Know of any pros and cons?

One resource that I have found so far seems to say there are good and bad times to use this

I know that I could use this or MainActivity.this, but that’s not the question.

..Just in case you don’t know what I’m talking about, here is a trvial example made up on the spot:

public class MainActivity extends Activity {
    MainActivity _activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _activity = this; // TODO: Find out if this is good practice?
        setContentView(R.layout.activity_main);
    }

    public void onClickButton(View v) {
        Toast.makeText(_activity, "Five boxing wizards", Toast.LENGTH_LONG).show();

        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(_activity, "asdf", Toast.LENGTH_LONG).show();
            }
        });
    }
}

EDIT: Another side-question for the comments: By a show of hands, who actually uses _activity?

  • 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-06-16T11:23:34+00:00Added an answer on June 16, 2026 at 11:23 am

    This is not good practice. Simply use this in most cases, and MainActivity.this when creating an anonymous subclass, etc.

    I think the right question to ask yourself is, “does adding this member variable do anything for me”, or “is there anything I can do with _activity that I can’t do with this. I can tell you the answer is “no”, but you should decide for yourself whether it is true.

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

Sidebar

Related Questions

Why sometimes I should use block and other times &block inside functions that accept
I don't know which title I should use for this question. I have a
I heard that static methods should use only static variables in java. But, main
Possible Duplicates: How effective is obfuscation? Protect ASP.NET Source code (Why) should I use
How should I use proguard with ormlite library on Android? Trying this: -keep class
My problem is that I don't know whether I should use multiple list view
I've read I should use money , but in todays fast paced world maybe
I know you should use POST whenever data will be modified on a public
I wonder how I should use the GTree (from GLib) to store data? Every
I know you should use a StringBuilder when concatenating strings but I was just

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.