如何让应用程序等待线程执行完毕?
How to make the application wait until a thread has executed?
在我的应用程序中,当 Activity 开始时,我必须将帐户列表从一个片段发送到另一个片段。我在以下线程中获取帐户列表,并将其保存在全局 ArrayList 中。如果对服务器的请求正常,ArrayList 会填充必要的信息。然后,在通过捆绑包将数据从一个片段传输到另一个片段之前,我调用了 loadAccounts 方法。问题是在我想在片段之间发送数据之前线程没有完成它的执行,因此在发送数据时 ArrayList 将为 NULL。
我怎样才能让应用程序等到线程执行,然后才将数据发送到另一个片段?
我的主题是这样的:
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();
}
在 onCreate 方法中我这样做:
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();
如有任何帮助,我们将不胜感激。谢谢
您不希望让您的应用程序等待,因为这会使您的应用程序变慢甚至在出现错误时卡住。
不要通过捆绑发送您的帐户。而是在您的 AccountsFragment
中创建一个方法
public void setAccounts(ArrayList<Account> accounts){
//do whatever you need with your accounts here
}
然后当你有 SUCCESSFUL_CODE、运行
时在你的 handleResult 方法中
fragment.setAccounts((ArrayList<Account>) accounts);
当然要做到这一点,请确保您的 AccountFragment 片段是一个字段而不是 onCreate 中的局部变量。还要确保在 运行 线程
之前实例化片段
如果您正在调用 Web 服务,请使用 AsyncTask
而不是线程并将其放入方法中
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();
并使用上下文
从 onPostExecute()
方法调用方法
在我的应用程序中,当 Activity 开始时,我必须将帐户列表从一个片段发送到另一个片段。我在以下线程中获取帐户列表,并将其保存在全局 ArrayList 中。如果对服务器的请求正常,ArrayList 会填充必要的信息。然后,在通过捆绑包将数据从一个片段传输到另一个片段之前,我调用了 loadAccounts 方法。问题是在我想在片段之间发送数据之前线程没有完成它的执行,因此在发送数据时 ArrayList 将为 NULL。 我怎样才能让应用程序等到线程执行,然后才将数据发送到另一个片段?
我的主题是这样的:
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();
}
在 onCreate 方法中我这样做:
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();
如有任何帮助,我们将不胜感激。谢谢
您不希望让您的应用程序等待,因为这会使您的应用程序变慢甚至在出现错误时卡住。
不要通过捆绑发送您的帐户。而是在您的 AccountsFragment
中创建一个方法public void setAccounts(ArrayList<Account> accounts){
//do whatever you need with your accounts here
}
然后当你有 SUCCESSFUL_CODE、运行
时在你的 handleResult 方法中fragment.setAccounts((ArrayList<Account>) accounts);
当然要做到这一点,请确保您的 AccountFragment 片段是一个字段而不是 onCreate 中的局部变量。还要确保在 运行 线程
之前实例化片段如果您正在调用 Web 服务,请使用 AsyncTask
而不是线程并将其放入方法中
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();
并使用上下文
从onPostExecute()
方法调用方法