2015-05-27 72 views
2

此代碼在旋轉屏幕時不起作用。 我嘗試使用處理程序,但消息與先前的活動(在旋轉之前)和新的活動分離。從AsyncTask更新UI並正確處理屏幕旋轉

¿線程如何將消息發送到新的活動? 請不要建議避免旋轉屏幕。

public class SampleActivity extends Activity { 
    TextView text; 
    Handler handler; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     LinearLayout layout = new LinearLayout(this); 
     layout.setOrientation(LinearLayout.VERTICAL); 
     text = new TextView(this); 
     text.setText("HELLO"); 

     layout.addView(text); 
     if (savedInstanceState != null) { 
      text.setText(savedInstanceState.getString("text")); 
     } 

     setContentView(layout); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putString("text", text.getText().toString()); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     new CounterTask().execute(); 
    } 

    public class CounterTask extends AsyncTask<Void, String, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 
      for (int i = 0; i < 100; i++) { 
       try { 
        Thread.sleep(100); 
        publishProgress("Hello " + i); 
       } catch (Exception e) { 

       } 
      } 
      return null; 
     } 

     @Override 
     protected void onProgressUpdate(String... values) { 
      String str = values[0]; 
      text.setText(str); 
     } 
    } 
} 

回答

2

您可以運行在保留片段AsyncTask

public class TaskFragment extends Fragment { 
    private Callback mCallback; 

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

     setRetainInstance(true); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     try { 
      mCallback = (Callback) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement TaskFragment.Callback"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 

     mCallback = null; 
    } 

    public void execute(){ 
     new CounterTask().execute(); 
    } 

    public interface Callback { 
     void onTaskUpdate(String value); 
    } 

    public class CounterTask extends AsyncTask<Void, String, Void> { 
     @Override 
     protected Void doInBackground(Void... params) { 
      for (int i = 0; i < 100; i++) { 
       try { 
        Thread.sleep(100); 
        publishProgress("Hello " + i); 
       } catch (Exception e) { 

       } 
      } 
      return null; 
     } 

     @Override 
     protected void onProgressUpdate(String... values) { 
      if(mCallback != null) { 
       String str = values[0]; 
       mCallback.onTaskUpdate(str); 
      } 
     } 
    } 
} 

然後,在您的活動中實施回調並通過片段管理器添加片段。

public class SampleActivity extends Activity implements 
     TaskFragment.Callback { 
    private static final String TAG_TASK_FRAGMENT = "task_fragment"; 

    private TaskFragment mTaskFragment; 

    private TextView text; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     LinearLayout layout = new LinearLayout(this); 
     layout.setOrientation(LinearLayout.VERTICAL); 
     text = new TextView(this); 
     text.setText("HELLO"); 

     layout.addView(text); 
     if (savedInstanceState != null) { 
      text.setText(savedInstanceState.getString("text")); 
     } 

     setContentView(layout); 

     FragmentManager fm = getFragmentManager(); 
     mTaskFragment = (TaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT); 

     if(mTaskFragment == null){ 
      mTaskFragment = new TaskFragment(); 
      fm.beginTransaction() 
        .add(mTaskFragment, TAG_TASK_FRAGMENT) 
        .commit(); 
     } 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putString("text", text.getText().toString()); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     mTaskFragment.execute(); 
    } 

    @Override 
    public void onTaskUpdate(String value) { 
     text.setText(value); 
    } 
} 
0

只需添加一些配置以防止在屏幕旋轉時重新創建活動,然後一切正常,因爲您的活動只有一個實例。

流動行添加到您的活動標籤在AndroidManifest.xml

android:configChanges="orientation|keyboardHidden|screenSize" 

而且這種方法對你SampleActivity.java

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    // when screen rotated, this method will be called instead of onCreate 
}