Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
AsyncTask sample (non-retain Fragment)
<?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>
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();
}
}
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();
}
}
@daichan4649
Owner

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.