【发布时间】:2014-12-11 04:10:58
【问题描述】:
我需要从 runnable 更新 ui。我的逻辑如下。 我从片段生命周期的 onCreate 开始运行。可运行实例负责请求网络。问题是可运行实例从网络获取数据后,我不知道如何更新片段。
在 CustomFragment.java 中的片段中开始运行的代码。
public void onCreate(Bundle savedInstanceState) {
Log.d(DEBUG_TAG, "onCreate");
super.onCreate(savedInstanceState);
accountMgr.requestAccountInfo();
}
在 AccountManager.java 中开始运行的代码
/**
* request Account info from server
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void requestAccountInfo() {
Account act = getCurrentAccount();
Thread t = new Thread(new RequestAccountInfoTask(act));
t.start();
}
/**
* automatically update Account info, like space usage, total space size, from background.
*/
class RequestAccountInfoTask implements Runnable {
private Account account;
public RequestAccountInfoTask(Account account) {
this.account = account;
}
@Override
public void run() {
doRequestAccountInfo(account);
}
}
【问题讨论】:
标签: java android multithreading android-fragments