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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:35:46+00:00 2026-05-25T12:35:46+00:00

I need to get a reference to the main Activity from a Service. This

  • 0

I need to get a reference to the main Activity from a Service.

This is my design:

MainActivity.java

public class MainActivity extends Activity{
private Intent myIntent;
onCreate(){
 myIntent=new Intent(MainActivity.this, MyService.class);

 btnStart.setOnClickListener(new OnClickListener(){
  public void onClick(View V){
   startService(myIntent);
   });
}}

MyService.java

class MyService extends Service{

 public IBinder onBind(Intent intent) {
  return null;
 }

 onCreate(){
 //Here I need to have a MainActivity reference
 //to pass it to another object
 }
}

How can I do this?

[EDIT]

Thanks to all for the answers!
This app is a web server, that at this moment works only with threads, and I want to use a service instead, to make it work also in the background.
The problem is that I have a class that is responsible for getting the page from assets, and to do this operation I need to use this method:

InputStream iS =myActivity.getAssets().open("www/"+filename); 

At this moment my project has only one Activity and no services, so I can pass the main activity’s reference directly from itself:

WebServer ws= new DroidWebServer(8080,this);

So, in order to make this app work with a service, what should I change in my design?

  • 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-25T12:35:47+00:00Added an answer on May 25, 2026 at 12:35 pm

    You didn’t explain why you need this. But this is definitely bad design. Storing references to Activity is the first thing you shouldn’t do with activities. Well, you can, but you must track Activity lifecycle and release the reference after its onDestroy() is called. If you are not doing this, you’ll get a memory leak (when configuration changes, for example). And, well, after onDestroy() is called, Activity is considered dead and is most likely useless anyway.

    So just don’t store the reference in Service. Describe what you need to achieve instead. I’m sure there are better alternatives out there.


    UPDATE

    Ok, so you do not actually need reference to Activity. Instead you need reference to Context (which in your case should be ApplicationContext to not keep reference to Activity or any other component for that matter).

    Assuming you have a separate class that handles WebService request:

    class WebService 
    {   
         private final Context mContext;
         public WebService(Context ctx) 
         {
            //The only context that is safe to keep without tracking its lifetime
            //is application context. Activity context and Service context can expire
            //and we do not want to keep reference to them and prevent 
            //GC from recycling the memory.
            mContext = ctx.getApplicationContext(); 
         }
    
         public void someFunc(String filename) throws IOException 
         {
             InputStream iS = mContext.getAssets().open("www/"+filename); 
         }
    }
    

    Now you can create & use WebService instance from Service (which is recommended for such background tasks) or even from Activity (which is much trickier to get right when web service calls or long background tasks are involved).

    An example with Service:

    class MyService extends Service
    {
        WebService mWs;
        @Override
        public void onCreate()
        {
            super.onCreate();
            mWs = new WebService(this);
    
           //you now can call mWs.someFunc() in separate thread to load data from assets.
        }
    
        @Override
        public IBinder onBind(Intent intent)
        {
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method inside an Activity which implements an ImageLoader class: public void
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I have standard class library infrastructure assembly which i reference in my main application.
i need to show images from sqlite database into gridview or gallery view. this
I need to get some links from the following code. I managed to reach
I have made a small custom view component: public class ActionBar extends RelativeLayout {
Possible Duplicate: Can I get a reference to the 'owner' class during the init
I need get all items these have no categories int? categoryId = null; var
I'm working with C#.net developing applications for windows mobile 6, and i need get
I have two tables with a weak relation. I need get a text value

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.