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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:51:47+00:00 2026-05-30T15:51:47+00:00

My main activity does some initial things and than starts InitDBActivity. This one is

  • 0

My main activity does some initial things and than starts InitDBActivity. This one is supposed to check i database is up to date and update it if necessary. The following is done in InitDBActivity’s onCreate method.

Thread asyncDBCreation = new Thread(new Runnable() {
                public void run() {

                    SQLiteDatabase db = null;
                    try{
                        db = dbh.getWritableDatabase();
                    }catch(Exception ex){
                        return;
                    }

                    if(dbh.recreateDB(db)){
                        try{
                            dbh.insertQueries(dbh.getQueries(getAssets()),db);
                        }catch(Exception ex){
                            dbh.close();
                            finish();
                            return;
                        }
                        dbh.close();

                        prefEdit.putLong(dbVersion, dbFileVersion);
                        prefEdit.commit();

                        startActivity(intent);
                        finish();
                    }
                    else{
                    }
                }
            });
            asyncDBCreation.start();

The problem is that if the user clicks the ‘back’ button he exits the app, but this thread is still running in background. It wold be fine – the job gets gone anyway, but… it turns out that if he launches the application again before this thread finishes – everything messes up. Data in database is multiplied. How can I kill this thread?

  • 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-30T15:51:49+00:00Added an answer on May 30, 2026 at 3:51 pm

    in java, dont have method can kill thread, only interrupt the thread, you can use method:thread.interrupt() to interrupt thread, but the thread must in the state which can be interrupted, like: sleep, or wait.

    so, here you can use db.close method to close database, the data query work will throw exception, you can catch the exception to finish the thread work.

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

Sidebar

Related Questions

The main activity includes some variables with set values. I created a sub-activity with
I have a widget that supposed to call an Activity of the main app
I have three activities 1 is the main activity 2 is some other activity
I am starting an activity from one by passing some data in the intent.
My activity A is a game and it does some background operations. When I
In my Android App I have a main activity and some other activities. The
I have an Activity that starts a Service. In my Activity: startService(new Intent(this, MyService.class));
i'm doing a location grabbing service for my phone. The main activity sets some
My main activity calls an AsyncTask when a user clicks a button. This AsyncTask
Being frustrated about fragment behavior, I started doing some testing. I have one activity

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.