0

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.

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
Phantom
  • 968
  • 3
  • 14
  • 32
  • »How to make the application wait until a thread has executed?« - Threads usually have a »join« method. – theV0ID Feb 03 '15 at 14:27
  • Check this thread, i guess it does contain every information that you need. http://stackoverflow.com/questions/861346/in-java-how-do-you-determine-if-a-thread-is-running – SomeJavaGuy Feb 03 '15 at 14:28
  • 1
    Have you considered using a `Queue` or better still a `BlockingQueue`? – OldCurmudgeon Feb 03 '15 at 14:36

2 Answers2

1

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

Klitos G.
  • 806
  • 5
  • 14
0

If you're calling web service use AsyncTaskinstead 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

Sjd
  • 1,261
  • 1
  • 12
  • 11