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

  • Home
  • SEARCH
  • 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 9122713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:10:02+00:00 2026-06-17T06:10:02+00:00

How do I call the functions provided by Activity class from a class that

  • 0

How do I call the functions provided by Activity class from a class that does not extend Activity? Theoretically, yes, if I don’t extend Activity I cannot directly use the functions provided by it. But is there a workaround provided for this? If not, are there replacements or alternative ways for these functions?

For example,
If my class extends Activity, I can call setContentView() to instantiate my layout xml file. But if my class extends some other class and doesn’t extend Activity, then I can use the LayoutInflater to do the task. But what about other functions like registerReceiver() ? How do I get the functionality of ‘registerReceiver()‘ from any other class , obviously I wouldn’t want every such class to extend Activity. Static access by “Activity.function_name” is also not possible as these functions are not static.

Certain services can be accessed from anywhere. For example ‘println()‘ or Log.e(),System functions can be called from anywhere, whenever needed. Is there a similar way for other critical functions?

Conclusion:

Pass Context to destination class. For accessing some functions however, type-casting the passed Context to Activity is required.

Both Changdeo’s and BT’s answers are correct.

Thanks.

  • 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-17T06:10:03+00:00Added an answer on June 17, 2026 at 6:10 am

    Although I have not found any documentation explicitly stating why, in every case where I have ever needed to do this, simply passing the Activity‘s Context is sufficient.

    For a Context called contextActivity passed into any function, the following will allow access to these member functions you require:

    ((Activity) contextActivity).<anyMemberFunction>
    

    Or if you need these functions in multiple cases it might be simplest just to do the following:

    Activity myActivity = (Activity) contextActivity;
    

    From there you can access these Activity member functions whenever you like by using:

    myActivity.<desiredFunction>;
    

    As I mentioned, I have never found any case where this hasn’t worked, but also no solid documentation saying this will always work. This is the trick I have seen consistently used though. If anyone has more to add, please do.

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

Sidebar

Related Questions

It's said that you should not call GUI functions from a thread, but I'm
Is it possible to call functions from class like this: $class = new class;
Is there a way to call functions within a class upon instantiation of that
I have been trying to call some functions from a library provided by a
I have a third party application that allows you to call C functions from
My goal is to be able to call functions that are inside my JQuery
I am trying to call native functions from the ApplicationServices framework on a mac
I have an activity that binds to a service. The service provides the functions
I'm currently building a program in C# which will call functions in provided python
Is it possible to call functions within a case statement? Example: (CASE WHEN (((POWER((1+x1/x2),Term)

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.