Created
March 8, 2018 18:31
-
-
Save antonioxtasis/94bd0930fe04abf64f649f2d9cefb382 to your computer and use it in GitHub Desktop.
Android - DialogFragment respond to Fragment
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 MyDialogFragment extends DialogFragment { | |
@Override | |
public void onCreate(final Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setStyle(STYLE_NO_TITLE, 0); | |
} | |
@Override | |
public void setupDialog(final Dialog dialog, int style) { | |
super.setupDialog(dialog, style); | |
View contentView = View.inflate(getContext(), R.layout.dialog_fragment_layout, null); | |
dialog.setContentView(contentView); | |
// Transparent background | |
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); | |
Button btn = (Button) dialog.findViewById(R.id.btn_action); | |
btn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.delete, 0, 0, 0); | |
btn.setOnClickListener(view -> { | |
dialog.dismiss(); | |
getTargetFragment().onActivityResult( | |
getTargetRequestCode(), | |
Activity.RESULT_OK, | |
new Intent().putExtra("label", "some response") | |
); | |
}); | |
} | |
} |
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 MyFragment extends Fragment { | |
public static final int CODE_FOR_MY_DIALOG_FRAGMENT = 88; | |
public MyFragment(){ } | |
public static Fragment newInstance() { | |
return new MyFragment(); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_layout, container, false); | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
} | |
@Override | |
public void onActivityCreated(@Nullable Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
someButton.setOnClickListener(view -> { | |
DialogFragment dialogFragment = new MyDialogFragment(); | |
dialogFragment.setTargetFragment(this, CODE_FOR_MY_DIALOG_FRAGMENT); | |
dialogFragment.show(getFragmentManager(), dialogFragment.getTag()); | |
}); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, final Intent data) { | |
switch (requestCode){ | |
case CODE_FOR_MY_DIALOG_FRAGMENT: | |
// stuff | |
break; | |
case CODE_ANOTHER: | |
// stuff | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment