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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:16:08+00:00 2026-05-18T20:16:08+00:00

I have service with thread, From activity I am calling start service(from Activity::onDestroy()) and

  • 0

I have service with thread, From activity I am calling start service(from Activity::onDestroy()) and stop service (activity::onCreate()).

StopService() returned instantly but my Service::OnDestroy called after so many time, pretty late, Why? How to make Service::OnDestroy called immediately when calling StopService().

Activity class

MyActivity::OnDestroy()
{
  System.out.println("MyActivity-OnDestroy Start");
  // we are leaving activity so start service
  Intent svcIntent = new Intent(this,MyService.class);
  svcIntent.putExtra("SomeData", m_iData);
  startService(svcIntent);
  System.out.println("MyActivity-OnDestroy, startservice called");
  ..
  ..
  System.out.println("MyActivity-OnDestroy, End");
}
MyActivity::OnCreate()
{
  System.out.println("MyActivity-onCreate Start");
  // if service running stop it.
  if (m_bCalledFromNotification)
  {
   System.out.println("Stopping Service");
   boolean b = stopService(new Intent(this,AudioService.class));
   System.out.println("Stopping Service, Status="+b);
  }
  System.out.println("MyActivity-onCreate End");      
}

Service class

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

// When service is created
@Override
public void onCreate() 
{
    // initialize service data
    System.out.println("MyService Created");    
}

@Override
public void onDestroy() 
{
    System.out.println("In MyService::onDestroy");
    m_UIUpdater.interrupt();
    System.out.println("In MyService::onDestroy, updater thread interrupted");
    ..
    System.out.println("My Service Stopped");
}

// This is the old onStart method that will be called on the pre-2.0
// platform.  On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) 
{
    StartupHandler(intent,startId);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    StartupHandler(intent,startId);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

public void StartupHandler(Intent intent, int startid) 
{
    // send notification 
    System.out.println("In MyService::StartupHandler, Sending notification");

    // start thread 
    m_UIUpdater = new Thread(new Runnable() 
        {
        public void run() 
        {
            System.out.println("In MyService::Thread::Run");
            while (!Thread.interrupted())
            {
                // check update
                System.out.println("In MyService::Thread::Run::got update and loop for next update");
            }
        }
    }
}

Now here is output of logcat which show how late MyService::OnDestroy called.

12-17 09:25:44.462: INFO/System.out(314): MyActivity-OnDestroy Start
..
12-17 09:25:44.493: INFO/System.out(314): MyActivity-OnDestroy, startservice called
..
12-17 09:25:51.102: INFO/System.out(314): MyActivity-OnDestroy, End 
12-17 09:25:51.153: INFO/System.out(314): MyService Created
12-17 09:25:51.172: INFO/System.out(314): In MyService::StartupHandler, Sending notification
12-17 09:25:51.273: INFO/System.out(314): In MyService::Thread::Run
12-17 09:25:51.302: INFO/System.out(314): In MyService::Thread::Run::got update and loop for next update
..
..
..
12-17 09:25:59.903: INFO/System.out(314): MyActivity-onCreate Start
..
12-17 09:26:00.461: INFO/System.out(314): Stopping Service
12-17 09:26:00.471: INFO/System.out(314): Stopping Service, Status=true
12-17 09:26:01.363: INFO/System.out(314): MyActivity-onCreate End
12-17 09:26:01.421: INFO/System.out(314): In MyService::OnDestroy
12-17 09:26:01.421: INFO/System.out(314): In MyService::OnDestroy, updater interrupted
12-17 09:26:01.591: INFO/System.out(314): My Service Stopped

Have i implemented service code correctly?

How can i wait for service to destroy gracefully so that i can do remaining process after service has destroyed, like reading data service has stopped.

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-05-18T20:16:08+00:00Added an answer on May 18, 2026 at 8:16 pm

    It seems to me that onDestroy is called when GC reclaims the service.

    Try m_UIUpdater.interrupt(); in your onStop.

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

Sidebar

Related Questions

I have service with thread, From activity I am calling start service(from Activity::onDestroy()) and
I have some problem with calling web service from flex. I have service with
I have a Service which starts a thread during onCreate() . The thread is
We have a Windows Service written in C#. The service spawns a thread that
I have a WCF service method that's running in a worker thread I spin
After following the great advice given in a thread about service beans I have
i have this thread which run as a service: public void run() { try
I have a background thread that handles communication with an external service. Each time
I have following code: public class readSensorsData extends Activity implements SensorListener { /** Called
I have an Android activity which in turn starts a thread. In the thread

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.