Last active
March 3, 2016 09:37
-
-
Save daichan4649/2480065 to your computer and use it in GitHub Desktop.
AsyncTask sample (non-retain Fragment)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
画面回転等で Activity の再生成が発生しても AsyncTask でのバックグラウンド処理を継続する
// 参考
yanzm本2
2.9 ビューを持たないフラグメントで定期処理をする