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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:57:29+00:00 2026-06-09T07:57:29+00:00

In my Main Activity, I have a Thread that is doing alot of stuff,

  • 0

In my Main Activity, I have a Thread that is doing alot of stuff, including adding some records to a database. In my second activity, which inherit from the Main Activity, I want to do a query to my database. But I need to check if the first thread in the Main Activity is finished, what I’ve done so far is:

public class History extends Main {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

    if(!(MainThread.isAlive())) {
            getFromDatabase();
    }
}
}

This is my getFromDatabase() method

    pd = ProgressDialog.show(this, "Please Wait",
                "Getting cases from database", false);

    Thread t = new Thread(this);
    t.start();

which will call this run method:

@Override
public void run() {

    ArrayList<Case> c = db1.getAllCases();
    Message msg = handler.obtainMessage();
    msg.obj = c;
    handler.sendMessage(msg);
}

private Handler handler = new Handler() {

    @SuppressWarnings("unchecked")
    @Override
    public void handleMessage(Message m) {
        pd.dismiss();
        list = (ArrayList<Case>) m.obj;
        tempList = getCaseNumberToTempList(list);
        tempCaseList = createTempList(list);

        lv.setAdapter(new CustomAdapter(History.this, list));
        lv.setTextFilterEnabled(true);
    }
};

But if I do so, the following line of code will crash my application, it will give a NullPointerException:

if(!(MainThread.isAlive())) {
            getFromDatabase();
    }

How can I be sure that that the first thread is finished with all the work before I query the database from my history activity?

  • 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-09T07:57:31+00:00Added an answer on June 9, 2026 at 7:57 am

    You can make the Thread in the getFromDatabase() method a static class level variable, write a static get method for it in your Activity, and check for isAlive() in your child Activity.

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

Sidebar

Related Questions

I am making an app in which i have a main activity that the
I have a main activity that launches: 1.- A network prone thread that writes
Splash I have main activity UI startup operations that take between 5-10 seconds (that
i have main activity in which i have Four menus. and i have one
I have a Tabhost which is my main activity. When a tab is clicked
I have a main activity that launches a child activity using the following code:
In my main activity, I would like to have it set up, so that
I have an activity (BookMarks) that is called from main activity. BookMarks throw an
I have a lot of buttons on main activity. Each button run this same
I have an android app where in my Main activity I can play music

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.