简体   繁体   中英

How to make the application wait until a thread has executed?

In my application, I have to send a list of accounts from one fragment to another, when the Activity starts. I get the list of accounts in the following thread, saving it in a global ArrayList. If the request to the server is good, the ArrayList gets populated with the necessary information. Then, i call the loadAccounts method before transferring data via bundle from one fragment to another. The problem is that the thread doesn't get to finish it's execution before I want to send the data between fragments, hence the ArrayList will be NULL when the data is sent. How can I make the application wait until the thread executes, and only after that to send the data to the other fragment ?

My thread looks like this:

public void loadAccounts() {

LoadAccounts loadAccountsThread = new LoadAccounts(new Handler() {

    public void handleResult(Result result) {
        switch (result) {
            case SUCCESSFUL_CODE:
                accountsList = (ArrayList<Account>) accounts;
                break;
            case FAILED_CODE:
                errorMsg = error.toString();
                showDialog(errorMsg);
            default:
                break;
        }
    }
});
loadAccountsThread.start();

}

while in the onCreate method I do this:

loadAccounts();
                Bundle args = new Bundle();
                AccountsFragment fragment = new AccountsFragment ();
                args.putSerializable("accounts", accountsList.get(0));
                fragment.setArguments(args);
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.rightConent, fragment).commit();

Any help would be appreciated. Thanks.

You do not want to make your application wait because it will make your app slow or even stuck in case of an error.

Do not send your accounts through the bundle. Instead create a method inside your AccountsFragment

public void setAccounts(ArrayList<Account> accounts){
    //do whatever you need with your accounts here
}

and then inside your handleResult method when you have a SUCCESSFUL_CODE, run

fragment.setAccounts((ArrayList<Account>) accounts);

of course to do this, make sure your AccountFragment fragment is a field and not a local variable inside your onCreate. Make sure also that you instantiate your Fragment before running your thread

If you're calling web service use AsyncTask instead of thread and put this inside a method

Bundle args = new Bundle();
            AccountsFragment fragment = new AccountsFragment ();
                args.putSerializable("accounts", accountsList.get(0));
                fragment.setArguments(args);
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.rightConent, fragment).commit(); 

and call the method from onPostExecute() method using context

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM