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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:38:33+00:00 2026-06-05T04:38:33+00:00

My application have some activities which in the first one I connect a socket

  • 0

My application have some activities which in the first one I connect a socket to communicate with a server on the others activities. This socket runs in a worker thread.

My question is: where can I close this socket when the application is finishing? Using the BACK button for instance…

I thought close the socket in onDestroy() of the last activity but this activity can be destroyed by the system at runtime and close the socket even if the application is not finishing. I don’t want this.

My run() method of the thread handling the socket connection is like:

public void run() { 
    if (this.bliveclient.isConnected()){       
        try {
            //...
            while (running) {
                //waiting for input data and do something...
            }
        }
        catch (IOException ex) {
            //handle exception
        }
        finally{
            try {
                mySocket.close();
            } catch (IOException ex) {
                //handle exception
            }
        }
    }

But the finally block is never called.

Can anyone give me some hint?

  • 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-05T04:38:36+00:00Added an answer on June 5, 2026 at 4:38 am

    The finally block is never executed because the run loop never completes. At some point Android just kills the process, which kills the VM and everything just goes away.

    If you have an application that just contains activities and you are doing network I/O in a separate thread then there is no way for you to know when to shut the thread down because the “application” is never “finished”. You need to define how the application “finishes”.

    A better way is to put this into a Service, because if Android wants to kill the process it will first call onDestroy() in the Service which will give you a chance to shutdown your thread properly.

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

Sidebar

Related Questions

I have developed one application which will have activities and one background service and
In my WPF application I have some drawing functionality. I have solved this using
In my Java application I have some copy-constructors like this public MyClass(MyClass src) {
I have created a play application and have some models. One of them contains
I am devloping smart device application in C#. In this application I have some
Ive got two activities in the same application. the first one is with gui(main
I have a GWT MVP application using Activities and Places. This is inspired by
I have an Android application which sends some data to a webservice. I need
I have an application which contains TextViews and ImageButtons organized in various layouts. This
We have developed one android application which will send the request and receive 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.