Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Last active February 9, 2017 11:16
Show Gist options
  • Select an option

  • Save SelvinPL/4230c0b6b45fb69a8ed856a4fa43dec3 to your computer and use it in GitHub Desktop.

Select an option

Save SelvinPL/4230c0b6b45fb69a8ed856a4fa43dec3 to your computer and use it in GitHub Desktop.
Show progress
public class TestFragment extends DialogFragment {
private static final String INNER_TAG = "TEST_INNER_TAG";
InnerFragment inner = null;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
inner = new InnerFragment();
inner.setParent(this);
getChildFragmentManager().beginTransaction().add(inner, INNER_TAG).commit();
} else {
inner = (InnerFragment)getChildFragmentManager().findFragmentByTag(INNER_TAG);
inner.setParent(this);
}
}
@Override
public void onPause() {
super.onPause();
inner.setParent(null);
}
public static class InnerFragment extends Fragment {
boolean close = false;
private TestFragment parent;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
//do some stuff on background
return null;
}
@Override
protected void onPostExecute(Void arg) {
//do some stuff on UI thread
if (parent == null)
close = true;
else
parent.dismiss();
}
}.execute();
}
public void setParent(TestFragment parent) {
this.parent = parent;
if (close)
parent.dismiss();
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new AlertDialog.Builder(getActivity(), R.style.AppTheme_Dialog_Alert).setTitle(R.string.please_wait_fragment_title)
.setView(R.layout.please_wait_fragment).setCancelable(false).create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
dialog.setCanceledOnTouchOutside(false);
((TextView) dialog.findViewById(R.id.please_wait_message)).setText(R.string.please_wait_message);
}
});
return dialog;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment