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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:29:45+00:00 2026-05-25T09:29:45+00:00

I am developing an application where I start a background service and I want

  • 0

I am developing an application where I start a background service and I want my main activity to perform a certain action when the service stops

I tried this in my service

public void onDestroy() {
    myActivity.getInstance().performAction();
}

however this causes timeout service exception as the service doesn’t stop except when this action is completed and this action takes a lot of time

how can I get around that . Any suggestions ?

  • 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-25T09:29:45+00:00Added an answer on May 25, 2026 at 9:29 am

    Service and Activity do not have the same lifecycle. This means your Service may find no instance of YourActivity when calling YourActivity.getInstance(). Your code should resist to that.

    And as performAction() is a method of YourActivity, it would certainly be better to execute it from YourActivity instead of executing it from your Service.

    So you should do as follows I think:

    • post a Message from your Service to YourActivity using a Handler (more details on Handler and on Message) instead of making a direct call to YourActivity.performAction() from your Service. You would send this message just before calling Service.stopSelf() (more details on stopSelf()) or why not from an override of this method.

    • make YourActivity able to handle this message and execute performAction() (you can find examples of how to handle messages here: just Ctrl-F “Handler” in this page). Execute it in a separate Thread to avoid the dreaded “Application Not Responding”.

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

Sidebar

Related Questions

I am new in developing application for WinCE 5.0. I want to start from
I am developing an application in which I start a service which will run
I'm developing an application that asks for a PIN when you start it. That's
HI, i am developing an application, where the user needs to enter the start
Before I start developing a desktop application, I was wondering how large the installer
We are developing a Winforms application and in the process of optimizing the start-up
I am about to start developing an Android application and had a question if
I am developing smart device application in C#. I am developing this application for
I am developing an application for music. I have an Activity where I have
Background I'm developing a research application that runs on Android phones. In short, the

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.