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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:45:26+00:00 2026-06-11T00:45:26+00:00

I don’t really get the idea behind how this whole thing works really, so

  • 0

I don’t really get the idea behind how this whole thing works really, so if I have some class A that need the context of a class B which extends Activity, how do i get that context?

I’m searching for a more efficient way than giving the context as a parameter to class A constructor. For example if class A is going to have millions of instances then we would end up having millions of redundant pointer to Context while we should be able somehow to have just one somewhere and a getter function…

  • 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-11T00:45:28+00:00Added an answer on June 11, 2026 at 12:45 am

    You can use Application class(public class in android.application package),that is:

    Base class for those who need to maintain global application state.
    You can provide your own implementation by specifying its name in your
    AndroidManifest.xml’s tag, which will cause that class
    to be instantiated for you when the process for your
    application/package is created.

    To use this class do:

    public class App extends Application {
    
        private static Context mContext;
    
        public static Context getContext() {
            return mContext;
        }
    
        public static void setContext(Context mContext) {
            this.mContext = mContext;
        }
    
        ...
    
    }
    

    In your manifest:

    <application
            android:icon="..."
            android:label="..."
            android:name="com.example.yourmainpackagename.App" >
                           class that extends Application ^^^
    

    In Activity B:

    public class B extends Activity {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.sampleactivitylayout);
    
            App.setContext(this);
                      ...
            }
    ...
    }
    

    In class A:

    Context c = App.getContext();
    

    Note:

    There is normally no need to subclass Application. In most situation,
    static singletons can provide the same functionality in a more modular
    way. If your singleton needs a global context (for example to register
    broadcast receivers), the function to retrieve it can be given a
    Context which internally uses Context.getApplicationContext() when
    first constructing the singleton.

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

Sidebar

Related Questions

I don't know: if this works. if it's a good idea. what it is
Don't know if this is possible, but I have some code like this: val
don't know better title for this, but here's my code. I have class user
Don't think that I'm mad, I understand how php works! That being said. I
Don't they both have to convert to machine code at some point to execute
Don't overthink this - there's a very commonly used term and I ... have
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't think my virtualhost is working correctly. This is what I have inside of
This could be a duplicate question, but I have no idea what search terms
Don't these two mean the same thing, first get the value and then increment?

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.