2017-01-09 65 views
0

我嘗試瞭解實現AsyncTask並將其附加到Fragment後面的基本設計模式。我跟許多教程和執行代碼(可提供無): 1)我的片段類,它包含的AsyncTask:附帶AsyncTask的Android片段在Activity中提交後爲空

public class MyFragment extends Fragment 
{ 
    public interface TaskCallback 
    { 
     void onPreExecute(); 
     void onProgressUpdate(Integer... i); 
     void onPostExecute(); 
    } 
    private TaskCallback mCallback; 
    private Task mTask; 

    @Override 
    public void onAttach(Activity activity) 
    { 
     super.onAttach(activity); 
     mCallback = (TaskCallback) activity; 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setRetainInstance(true); 

     mTask = new Task(); 
     mTask.execute(); 
    } 

    private class Task extends AsyncTask<Void, Integer, Void> 
    { 
     @Override 
     protected void onPreExecute() 
     { 
      mCallback.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) 
     { 
      for (int i=0; i<100; i++) 
      { 
       publishProgress(i*10); 
      } 
      return null; 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) 
     { 
      mCallback.onProgressUpdate(values); 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) 
     { 
      mCallback.onPostExecute(); 
     } 
    } 
} 

2)我的主要活動代碼

public class MainActivity extends ActionBarActivity implements MyFragment.TaskCallback 
{ 
    private ProgressBar mProgress; 
    private MyFragment mTaskFragment; 
    private final static String TAG_FRAGMENT = "Fragment Task"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mProgress = (ProgressBar) findViewById(R.id.progressBar1); 
     mProgress.setProgress(0); 

     //create fragment manager and fragment instance 
     FragmentManager mFM = getSupportFragmentManager(); 
     mTaskFragment = (MyFragment) mFM.findFragmentByTag(TAG_FRAGMENT); 

     if(mFM == null) 
     { 
      mTaskFragment = new MyFragment(); 
      mFM.beginTransaction().add(mTaskFragment, TAG_FRAGMENT).commit(); 
     } 
//I added this method, but to no help, the same without this method 
     mFM.executePendingTransactions(); 
    } 


    @Override 
    public void onPreExecute() 
    { 
     mProgress.setVisibility(View.VISIBLE); 
    } 

    @Override 
    public void onProgressUpdate(Integer... i) 
    { 
     mProgress.setProgress(i[0]); 
    } 

    @Override 
    public void onPostExecute() 
    { 
     mProgress.setVisibility(View.INVISIBLE); 
    } 
} 

基本上,當運行的代碼調試,

mTaskFragment = (MyFragment) mFM.findFragmentByTag(TAG_FRAGMENT); 
    if(mFM == null) 
    { 
     mTaskFragment = new MyFragment(); 
     mFM.beginTransaction().add(mTaskFragment, TAG_FRAGMENT).commit(); 
    } 

給人mTaskFragment = null

我想這裏是問題(mTaskFragment = null),代碼不會創建MyFragment的實例。
問題:如何更改代碼以使用此(Fragment + AsyncTask)模式從後臺更新ProgressBar?
另一個問題:片段onAttached(Activity a)已被棄用,現在我們應該使用onAttached(上下文上下文),這是否意味着它應該被實現爲:mCallback = (TaskCallback) context;

回答

0

if(mFM == null)替換爲if(mTaskFragment == null)您要檢查片段是否爲null而不是FragmentManager。

而且對於更新的方法:

public void onAttach(Activity activity)public void onAttach(Context context)

使用:mCallback = (TaskCallback) context;是細如ContextActivty一個超類(只要確保你的活動是實現該接口)

+0

謝謝! 'if(mTaskFragment == null)'解決了這個問題。令人尷尬的是它有多簡單,以及我多少努力找到它。 –

+0

沒問題,更新的答案包括你的其他問題 –