public class HomePageActivity extends AppCompatActivity {
public String[] friends;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Activity functions
FriendWorker fw = new FriendWorker();
fw.execute(...);
}
public void setFriends(String[] friends) {
this.friends = Arrays.copyOf(friends, friends.length);
}
public class FriendWorker extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
//get and return result
}
@Override
protected void onPostExecute(String result) {
setFriends(friendsArray);
}
}
}
我知道我从doInBackground获得了正确的结果,而onPostExecute确实调用并设置了我在HomePageActivity中声明的friends数组,但是当它退出FriendWorker时它并没有保留它设置的值。我认为这就是同步问题的原因。我没有太多的同步经验或异步会感激任何帮助。