FragmentTabHost和异步加载数据

时间:2013-06-15 22:11:29

标签: android android-layout android-fragments

我有2个标签的FragmentTabHost。 每个片段(标签)都需要从互联网加载数据并显示它。

如果我在onCreate上加载数据并填充它然后切换到另一个选项卡然后我有空视图。

TextView emailView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View convertView = inflater.inflate(R.layout.contactdetails_pro, container, false);

    emailView = (TextView) convertView.findViewById(R.id.contactdetails_info_email);

    return convertView;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String id = getArguments().getString("id");

    MyTask pdthread = new MyTask(this.getActivity(), id, new AsyncTaskCompleteListener() {
        @Override
        public void onComplete(Detail detail) {
            emailView.setText(detail.getEmail());
        }
    });
    pdthread.execute();
}

1 个答案:

答案 0 :(得分:0)

我对FragmentTabHost不熟悉,但您可以尝试这一点(注意:您可能需要制作Detail类工具Serializable):

Detail mDetail = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String id = getArguments().getString("id");

    if (savedInstanceState == null) {
        MyTask pdthread = new MyTask(this.getActivity(), id, new AsyncTaskCompleteListener() {
            @Override
            public void onComplete(Detail detail) {
                mDetail = detail;
                emailView.setText(mDetail.getEmail());
            }
        });
        pdthread.execute();
    } else {
        mDetail = (Detail)savedInstanceState.getSerializable("detail");
        if (mDetail ! = null) {
            emailView.setText(detail.getEmail());
        }
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("detail", mDetail);
}