Last active
August 1, 2018 20:29
-
-
Save daichan4649/6421407 to your computer and use it in GitHub Desktop.
AsyncTask + ProgressDialogFragment (Android)
This file contains 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
public class ProgressDialogFragment extends DialogFragment { | |
public final static String TITLE = "title"; | |
public final static String MESSAGE = "message"; | |
public final static String MAX = "max"; | |
public final static String CANCELABLE = "cancelable"; | |
public static ProgressDialogFragment newInstance() { | |
return new ProgressDialogFragment(); | |
} | |
public interface ProgressDialogFragmentListener { | |
void onProgressCancelled(); | |
} | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
// キャンセル設定(DialogFragment) | |
boolean cancelable = getArguments().getBoolean(CANCELABLE, false); | |
setCancelable(cancelable); | |
// ProgressDialog | |
String title = getArguments().getString(TITLE); | |
String message = getArguments().getString(MESSAGE); | |
ProgressDialog dialog = new ProgressDialog(getActivity()); | |
dialog.setTitle(title); | |
dialog.setMessage(message); | |
dialog.setIndeterminate(false); | |
// 進捗 | |
// dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); | |
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); | |
dialog.setMax(getArguments().getInt(MAX)); | |
return dialog; | |
} | |
public void updateProgress(int value) { | |
ProgressDialog dialog = (ProgressDialog) getDialog(); | |
if (dialog != null) { | |
dialog.setProgress(value); | |
} | |
} | |
@Override | |
public void onCancel(DialogInterface dialog) { | |
super.onCancel(dialog); | |
if (getProgressDialogFragmentListener() != null) { | |
getProgressDialogFragmentListener().onProgressCancelled(); | |
} | |
} | |
private ProgressDialogFragmentListener getProgressDialogFragmentListener() { | |
if (getActivity() == null) { | |
return null; | |
} | |
if (getActivity() instanceof ProgressDialogFragmentListener) { | |
return (ProgressDialogFragmentListener) getActivity(); | |
} | |
return null; | |
} | |
} |
This file contains 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
public class TestActivity extends Activity implements ProgressDialogFragmentListener { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// TestFragment | |
FragmentManager fragmentManager = getFragmentManager(); | |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | |
Fragment fragment = new TestFragment(); | |
fragmentTransaction.add(android.R.id.content, fragment, "test"); | |
fragmentTransaction.commit(); | |
} | |
// ProgressDialogFragmentListener | |
@Override | |
public void onProgressCancelled() { | |
Fragment fragment = getFragmentManager().findFragmentByTag("test"); | |
if (fragment instanceof ProgressDialogFragmentListener) { | |
((ProgressDialogFragmentListener) fragment).onProgressCancelled(); | |
} | |
} | |
} |
This file contains 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
public class TestFragment extends Fragment implements ProgressDialogFragmentListener { | |
public class TestTask extends AsyncTask<String, Integer, Void> { | |
@Override | |
protected void onPreExecute() { | |
showProgress(); | |
} | |
@Override | |
protected Void doInBackground(String... params) { | |
try { | |
Thread.sleep(500); | |
updateProgress(30); | |
Thread.sleep(500); | |
updateProgress(60); | |
Thread.sleep(500); | |
updateProgress(90); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onProgressUpdate(Integer... values) { | |
updateProgress(values); | |
} | |
@Override | |
protected void onPostExecute(Void unused) { | |
Intent intent = new Intent(getActivity(), ScheduleActivity.class); | |
startActivity(intent); | |
getActivity().finish(); | |
hideProgress(); | |
} | |
} | |
// Progress関係 | |
private void showProgress() { | |
Bundle args = new Bundle(); | |
args.putString(ProgressDialogFragment.TITLE, "タイトル"); | |
args.putString(ProgressDialogFragment.MESSAGE, "処理中 ..."); | |
args.putBoolean(ProgressDialogFragment.CANCELABLE, false); | |
args.putInt(ProgressDialogFragment.MAX, 100); | |
DialogFragment dialog = ProgressDialogFragment.newInstance(); | |
dialog.setArguments(args); | |
dialog.show(getActivity().getFragmentManager(), FragmentType.PROGRESS.getTag()); | |
} | |
private void updateProgress(Integer... values) { | |
ProgressDialogFragment progress = getProgressDialogFragment(); | |
if (progress == null) { | |
return; | |
} | |
progress.updateProgress(values[0]); | |
} | |
private void hideProgress() { | |
ProgressDialogFragment progress = getProgressDialogFragment(); | |
if (progress == null) { | |
return; | |
} | |
progress.dismissAllowingStateLoss(); | |
} | |
private ProgressDialogFragment getProgressDialogFragment() { | |
Fragment fragment = getFragmentManager().findFragmentByTag(FragmentType.PROGRESS.getTag()); | |
return (ProgressDialogFragment) fragment; | |
} | |
// ProgressDialogFragmentListener | |
@Override | |
public void onProgressCancelled() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment