So there is an activation, on it the Start button, on pressing the button DialogFragment is started. In DialogFragment, one text and 2 buttons (OK, Cancel). Need a dialog to enter a password. If the password is entered, the service starts (the service start is written on the start button). If canceled, I want the code to not be executed after the dialog has been called (service call). I can not think of a correct way to get cancellation from the dialogue. So far, I thought that the cancellation would throw an exception, but it seems it would not be correct.
- Isn't it easier to start the service after the correct password? because otherwise you need to stop him, for me you solve two problems right away. If not, just stop the service. - Shwarz Andrei
- you can get any data from DialogFragment via the interface - Jarvis_J
- @AbrogPetrovich what? - Alexander Alekseev
- If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
|
1 answer
In the DialogFragment
class:
public interface OnButtonClick { void onDialogClickListener(int action, int type); } OnButtonClick onClick; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { onClick = (OnButtonClick) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement onDialogClickListener"); } } @Override public void onDetach() { super.onDetach(); onClick = null; }
In MainActivity
:
...extends AppCompatActivity implements MyDialog.OnButtonClick {... @Override public void onDialogClickListener(int action, int type) {}
Usage: in DialogFragment
at the right time -
onClick.onDialogClickListener(action, type);
You can transfer absolutely any data.
- oneThank you very much for the answer. But in order for me to work, I had to change the Attach method: <pre> public void onAttach (Activity activity) {super.onAttach ((Activity) activity); try {onClick1 = (OnButtonClick) activity; } catch (ClassCastException e) {throw new ClassCastException (activity.toString () + "must implement onDialogClickListener"); }} </ pre> - Alexander Alekseev
- Yes, a really good decision, but you need to correct the answer and change the
context
to theactivity
- Aleksey Timoshchenko - And how with this approach to get a line from
EditText
? Here I have a question for me. I just need to transfer this line to goo.gl/trDVTR using your answer. Advise how to do ... - Aleksey Timoshchenko - if I correctly understood what you are
String myText = myEditText.getText().toString()
about, then in the usual way:String myText = myEditText.getText().toString()
and in the interface transfer thisString
instead ofint
- Jarvis_J
|