是否可以在后台线程内交换片段

时间:2012-12-30 11:38:49

标签: java android asynchronous android-asynctask android-fragments

我的想法是在AsyncTask中创建新片段,然后在onPostExecute上我将它与显示的进度条片段交换。我的问题是片段永远不会被交换,因此它与进度条片段保持一致。这不可能吗?这是一个糟糕的方法吗?我应该在DetailFragment中有进度条吗? (将会有很多带有进度条的碎片)

@Override
protected void onStart() {      
    super.onStart();
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new ProgressBarFragment()).commit();
    TestSwapFragment d = new TestSwapFragment();
    d.execute(data);
}

class TestSwapFragment extends AsyncTask<FragmentData, Void, MyFragment> {

FragmentData data = null;

    @Override
    protected MyFragment doInBackground(FragmentData... params) {

        try {
            Thread.sleep(15000); // Simulate the long running method below
        } catch (InterruptedException e) {                  
            e.printStackTrace();
        }

        this.data = params[0];
        DetailFragment df= new DetailFragment();
        df.ExecuteFragment(this.data); // Long running

        return df;
    }

    @Override
    protected void onPostExecute(MyFragment result) {               
        super.onPostExecute(result);

        if (this.data.Position == FragmentPosition.PaneOne)
                getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, result).commit();
        else
                getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame2, result).commit();
    }

}

1 个答案:

答案 0 :(得分:2)

我认为没有理由说明为什么这不起作用。但是说,这对我来说是一个非常奇怪的方法。

AsyncTask.doInBackground()通常用于创建/下载/处理数据,AyncTask.onPostExecute()用于更新UI。但是你要在这两种方法上分发UI部分。

此外,Fragment是一个UI类,您正在使用它来进行数据处理,再次混合每个类的目的。作为一般规则,在我的项目中,我倾向于遵循类似的东西(就像示例显示一些通用名称,但在真实的东西上,使用有意义的名称,我也可能忽略一些构造函数,只是为了保持简短)< / p>

MyActivity.java
public class MyActivity extends Activity{


      private class MyWorker extends AsyncTask<SomeDataObject, Integer, SomeDataListObject>{
            @Override
            protected MyFragment doInBackground(SomeDataObject... params) {
                 // in here we use SomeDataObject to generate the SomeDataListObject
                 return MyDataCruncher.processData(someDataObject);
                 // see it's a static method, process A and generates B, that's all.
                 // but very important that it's its own class, separate from Activity and Fragment
            }

            @Override
            protected void onPostExecute(SomeDataListObject result){
                if(results != null){
                     // Here we create the fragment, pass the data to it and do the transaction
                     getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, MyFrag.newInstance(result)).commit();

                }else{ /* do some error handling or whatever */ }
            }
}

然后在你的片段上:

public class MyFrag extends Fragment{
    private static String KEY_DATA = "_key_data_"
    public static MyFrag newInstance(SomeDataListObject data){
        MyFrag frag = new MyFrag();
        Bundle b = new Bundle();
        frag.setArguments(b);
        // it's important to make your data parcelable and pass it with a Bundle as part of the fragment life-cycle
        b.putParcelableArrayList(KEY_DATA, data);
        return frag;
    }

    // then inside onCreateView
     SomeDataListObject data = getArguments.getParcelableArray(KEY_DATA);
    // ---

}

希望有所帮助