public
Last active

AsyncTask sample (non-retain Fragment)

  • Download Gist
MainActivity.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
package test.fragment.asynctask;
 
import test.fragment.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
 
public class MainActivity extends Activity {
 
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(getClass().getName(), "[onCreate(activity)]");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
findViewById(R.id.update).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startUpdateText();
}
});
}
 
@Override
protected void onDestroy() {
Log.d(getClass().getName(), "[onDestroy(activity)]");
super.onDestroy();
}
 
private void startUpdateText() {
UpdateTextFragment fragment = UpdateTextFragment.newInstance();
getFragmentManager().beginTransaction().add(fragment, "updateText").commit();
fragment.startUpdateText();
}
}
UpdateTextFragment.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
package test.fragment.asynctask;
 
import test.fragment.R;
import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
 
public class UpdateTextFragment extends Fragment {
 
public static UpdateTextFragment newInstance() {
return new UpdateTextFragment();
}
 
private AsyncTask<Void, String, Void> task;
 
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(getClass().getName(), "[onCreate]");
super.onCreate(savedInstanceState);
 
// Activity再生成時に Activity#onCreate/onDestroy が走らないようにする
setRetainInstance(true);
}
 
@Override
public void onDestroy() {
Log.d(getClass().getName(), "[onDestroy]");
super.onDestroy();
 
if (task != null) {
task.cancel(true);
}
}
 
/**
* テキスト更新処理(非同期)
*/
public void startUpdateText() {
task = new UpdateAsyncTask();
task.execute();
}
 
private class UpdateAsyncTask extends AsyncTask<Void, String, Void> {
 
@Override
protected Void doInBackground(Void... params) {
while (!isCancelled()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
 
// テキスト更新
publishProgress("time: " + System.currentTimeMillis());
}
return null;
}
 
@Override
protected void onProgressUpdate(String... values) {
updateTextView(values[0]);
}
 
private void updateTextView(String text) {
final Activity activity = getActivity();
if (activity == null) {
return;
}
 
final TextView textView = ((TextView) activity.findViewById(R.id.text));
if (textView == null) {
return;
}
textView.setText(text);
}
}
 
@Override
public void onAttach(Activity activity) {
Log.d(getClass().getName(), "[onAttach]");
super.onAttach(activity);
}
 
@Override
public void onDetach() {
Log.d(getClass().getName(), "[onDetach]");
super.onDetach();
}
}
main.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
 
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start updateText" />
 
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
 
</LinearLayout>

画面回転等で Activity の再生成が発生しても AsyncTask でのバックグラウンド処理を継続する

// 参考
yanzm本2
2.9 ビューを持たないフラグメントで定期処理をする

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.