Skip to content

Instantly share code, notes, and snippets.

@mkaarthick
Forked from daichan4649/MainActivity.java
Created March 3, 2016 09:37
Show Gist options
  • Save mkaarthick/67db2cb9f99f607fa4d1 to your computer and use it in GitHub Desktop.
Save mkaarthick/67db2cb9f99f607fa4d1 to your computer and use it in GitHub Desktop.
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment