Guest User

Untitled

a guest
Oct 17th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. /**
  2. * This Activity displays the screen's UI, creates a TaskFragment
  3. * to manage the task, and receives progress updates and results
  4. * from the TaskFragment when they occur.
  5. */
  6. public class MainActivity extends Activity implements TaskFragment.TaskCallbacks {
  7.  
  8. private static final String TAG_TASK_FRAGMENT = "task_fragment";
  9.  
  10. private TaskFragment mTaskFragment;
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16.  
  17. FragmentManager fm = getFragmentManager();
  18. mTaskFragment = (TaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);
  19.  
  20. // If the Fragment is non-null, then it is currently being
  21. // retained across a configuration change.
  22. if (mTaskFragment == null) {
  23. mTaskFragment = new TaskFragment();
  24. fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit();
  25. }
  26.  
  27. // TODO: initialize views, restore saved state, etc.
  28. }
  29.  
  30. // The four methods below are called by the TaskFragment when new
  31. // progress updates or results are available. The MainActivity
  32. // should respond by updating its UI to indicate the change.
  33.  
  34. @Override
  35. public void onPreExecute() { ... }
  36.  
  37. @Override
  38. public void onProgressUpdate(int percent) { ... }
  39.  
  40. @Override
  41. public void onCancelled() { ... }
  42.  
  43. @Override
  44. public void onPostExecute() { ... }
  45. }
  46.  
  47.  
  48. // fragment
  49. /**
  50. * This Fragment manages a single background task and retains
  51. * itself across configuration changes.
  52. */
  53. public class TaskFragment extends Fragment {
  54.  
  55. /**
  56. * Callback interface through which the fragment will report the
  57. * task's progress and results back to the Activity.
  58. */
  59. interface TaskCallbacks {
  60. void onPreExecute();
  61. void onProgressUpdate(int percent);
  62. void onCancelled();
  63. void onPostExecute();
  64. }
  65.  
  66. private TaskCallbacks mCallbacks;
  67. private DummyTask mTask;
  68.  
  69. /**
  70. * Hold a reference to the parent Activity so we can report the
  71. * task's current progress and results. The Android framework
  72. * will pass us a reference to the newly created Activity after
  73. * each configuration change.
  74. */
  75. @Override
  76. public void onAttach(Activity activity) {
  77. super.onAttach(activity);
  78. mCallbacks = (TaskCallbacks) activity;
  79. }
  80.  
  81. /**
  82. * This method will only be called once when the retained
  83. * Fragment is first created.
  84. */
  85. @Override
  86. public void onCreate(Bundle savedInstanceState) {
  87. super.onCreate(savedInstanceState);
  88.  
  89. // Retain this fragment across configuration changes.
  90. setRetainInstance(true);
  91.  
  92. // Create and execute the background task.
  93. mTask = new DummyTask();
  94. mTask.execute();
  95. }
  96.  
  97. /**
  98. * Set the callback to null so we don't accidentally leak the
  99. * Activity instance.
  100. */
  101. @Override
  102. public void onDetach() {
  103. super.onDetach();
  104. mCallbacks = null;
  105. }
  106.  
  107. /**
  108. * A dummy task that performs some (dumb) background work and
  109. * proxies progress updates and results back to the Activity.
  110. *
  111. * Note that we need to check if the callbacks are null in each
  112. * method in case they are invoked after the Activity's and
  113. * Fragment's onDestroy() method have been called.
  114. */
  115. private class DummyTask extends AsyncTask<Void, Integer, Void> {
  116.  
  117. @Override
  118. protected void onPreExecute() {
  119. if (mCallbacks != null) {
  120. mCallbacks.onPreExecute();
  121. }
  122. }
  123.  
  124. /**
  125. * Note that we do NOT call the callback object's methods
  126. * directly from the background thread, as this could result
  127. * in a race condition.
  128. */
  129. @Override
  130. protected Void doInBackground(Void... ignore) {
  131. for (int i = 0; !isCancelled() && i < 100; i++) {
  132. SystemClock.sleep(100);
  133. publishProgress(i);
  134. }
  135. return null;
  136. }
  137.  
  138. @Override
  139. protected void onProgressUpdate(Integer... percent) {
  140. if (mCallbacks != null) {
  141. mCallbacks.onProgressUpdate(percent[0]);
  142. }
  143. }
  144.  
  145. @Override
  146. protected void onCancelled() {
  147. if (mCallbacks != null) {
  148. mCallbacks.onCancelled();
  149. }
  150. }
  151.  
  152. @Override
  153. protected void onPostExecute(Void ignore) {
  154. if (mCallbacks != null) {
  155. mCallbacks.onPostExecute();
  156. }
  157. }
  158. }
  159. }
Add Comment
Please, Sign In to add comment