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.