Pass Information from AlertDialog to parent FragmentIs Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I call one constructor from another in Java?How to get an enum value from a string value in Java?How do I pass a variable by reference?Stop EditText from gaining focus at Activity startupfindViewById in FragmentAndroid AlertDialog with DialogFragment: don't close the dialog even if OK is clickedCheck number of arguments passed to a Bash scriptsetText on button from another activity android

How to maximize the drop odds of the Essences in Diablo II?

Papers on arXiv solving the same problem at the same time

Can you cast bonus action and reaction spells while already casting a spell?

Do Bayesian credible intervals treat the estimated parameter as a random variable?

Why do these two functions have the same bytecode when disassembled under dis.dis?

Does ostensible/specious make sense in this sentence?

Discussing work with supervisor in an invited dinner with his family

Redacting URLs as an email-phishing preventative?

Higman's lemma and a manuscript of Erdős and Rado

What should come first—characters or plot?

Why are non-collision-resistant hash functions considered insecure for signing self-generated information

How do proponents of Sola Scriptura address the ministry of those Apostles who authored no parts of Scripture?

How is linear momentum conserved in case of a freely falling body?

I don't have the theoretical background in my PhD topic. I can't justify getting the degree

Why is the UK so keen to remove the "backstop" when their leadership seems to think that no border will be needed in Northern Ireland?

Architectural feasibility of a tiered circular stone keep

Movie where people enter a church but find they can't leave, not in English

To get so rich that you are not in need of anymore money

Changing JPEG to RAW to use on Lightroom?

What is the loud noise of a helicopter when the rotors are not yet moving?

How many lines of code does the original TeX contain?

Command "root" and "subcommands"

Nothing like a good ol' game of ModTen

How can I download a file through 2 SSH connections?



Pass Information from AlertDialog to parent Fragment


Is Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I call one constructor from another in Java?How to get an enum value from a string value in Java?How do I pass a variable by reference?Stop EditText from gaining focus at Activity startupfindViewById in FragmentAndroid AlertDialog with DialogFragment: don't close the dialog even if OK is clickedCheck number of arguments passed to a Bash scriptsetText on button from another activity android






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to pass Information from my AlertDialog to the parent Fragment it is on. But as soon as you klick the positive button, the app will crash.
I dont really know what todo anymore already read many Posts and articles but could not find the Problem.
Would be great if you could help me out.(Im a beginner)



Here is the Error I get with the first Problem in Code and the second commented.



E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 20682
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.test.SchulfachDialog$SchulfachDialogListener.applyTexts(java.lang.String)' on a null object reference
at com.example.test.SchulfachDialog$1.onClick(SchulfachDialog.java:39)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)


here is my code for the alert Dialog:



builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)


)
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String name = editTextName.getText().toString();
listener.applyTexts(name); // Problem 1: when positiv Button is pushed this line causes a crash

);

editTextName = view.findViewById(R.id.edit_name);

return builder.create();
}


this is the applyTexts which I override in the fragments Code:



public interface SchulfachDialogListener
void applyTexts(String name);


}


@Override public void applyTexts(String name)
test = name;


}


I also have this block in which the 2 commented lines cause a crash when clicking on the button which starts the alert Dialog:



public void onAttach(Context context) 
super.onAttach(context);

try
listener = (SchulfachDialogListener) context;
catch (ClassCastException e)
// throw new ClassCastException(context.toString()+
// "must implement SchulfachDialogListener");











share|improve this question
























  • listener.applyTexts(name); here listener is null. share the code from where (your parent Fragment) u set your listener

    – Solaiman Hossain
    Mar 28 at 5:26

















0















I am trying to pass Information from my AlertDialog to the parent Fragment it is on. But as soon as you klick the positive button, the app will crash.
I dont really know what todo anymore already read many Posts and articles but could not find the Problem.
Would be great if you could help me out.(Im a beginner)



Here is the Error I get with the first Problem in Code and the second commented.



E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 20682
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.test.SchulfachDialog$SchulfachDialogListener.applyTexts(java.lang.String)' on a null object reference
at com.example.test.SchulfachDialog$1.onClick(SchulfachDialog.java:39)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)


here is my code for the alert Dialog:



builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)


)
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String name = editTextName.getText().toString();
listener.applyTexts(name); // Problem 1: when positiv Button is pushed this line causes a crash

);

editTextName = view.findViewById(R.id.edit_name);

return builder.create();
}


this is the applyTexts which I override in the fragments Code:



public interface SchulfachDialogListener
void applyTexts(String name);


}


@Override public void applyTexts(String name)
test = name;


}


I also have this block in which the 2 commented lines cause a crash when clicking on the button which starts the alert Dialog:



public void onAttach(Context context) 
super.onAttach(context);

try
listener = (SchulfachDialogListener) context;
catch (ClassCastException e)
// throw new ClassCastException(context.toString()+
// "must implement SchulfachDialogListener");











share|improve this question
























  • listener.applyTexts(name); here listener is null. share the code from where (your parent Fragment) u set your listener

    – Solaiman Hossain
    Mar 28 at 5:26













0












0








0








I am trying to pass Information from my AlertDialog to the parent Fragment it is on. But as soon as you klick the positive button, the app will crash.
I dont really know what todo anymore already read many Posts and articles but could not find the Problem.
Would be great if you could help me out.(Im a beginner)



Here is the Error I get with the first Problem in Code and the second commented.



E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 20682
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.test.SchulfachDialog$SchulfachDialogListener.applyTexts(java.lang.String)' on a null object reference
at com.example.test.SchulfachDialog$1.onClick(SchulfachDialog.java:39)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)


here is my code for the alert Dialog:



builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)


)
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String name = editTextName.getText().toString();
listener.applyTexts(name); // Problem 1: when positiv Button is pushed this line causes a crash

);

editTextName = view.findViewById(R.id.edit_name);

return builder.create();
}


this is the applyTexts which I override in the fragments Code:



public interface SchulfachDialogListener
void applyTexts(String name);


}


@Override public void applyTexts(String name)
test = name;


}


I also have this block in which the 2 commented lines cause a crash when clicking on the button which starts the alert Dialog:



public void onAttach(Context context) 
super.onAttach(context);

try
listener = (SchulfachDialogListener) context;
catch (ClassCastException e)
// throw new ClassCastException(context.toString()+
// "must implement SchulfachDialogListener");











share|improve this question














I am trying to pass Information from my AlertDialog to the parent Fragment it is on. But as soon as you klick the positive button, the app will crash.
I dont really know what todo anymore already read many Posts and articles but could not find the Problem.
Would be great if you could help me out.(Im a beginner)



Here is the Error I get with the first Problem in Code and the second commented.



E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 20682
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.test.SchulfachDialog$SchulfachDialogListener.applyTexts(java.lang.String)' on a null object reference
at com.example.test.SchulfachDialog$1.onClick(SchulfachDialog.java:39)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)


here is my code for the alert Dialog:



builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)


)
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String name = editTextName.getText().toString();
listener.applyTexts(name); // Problem 1: when positiv Button is pushed this line causes a crash

);

editTextName = view.findViewById(R.id.edit_name);

return builder.create();
}


this is the applyTexts which I override in the fragments Code:



public interface SchulfachDialogListener
void applyTexts(String name);


}


@Override public void applyTexts(String name)
test = name;


}


I also have this block in which the 2 commented lines cause a crash when clicking on the button which starts the alert Dialog:



public void onAttach(Context context) 
super.onAttach(context);

try
listener = (SchulfachDialogListener) context;
catch (ClassCastException e)
// throw new ClassCastException(context.toString()+
// "must implement SchulfachDialogListener");








java android parameter-passing android-alertdialog






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 19:15









EnigmaEnigma

51 bronze badge




51 bronze badge















  • listener.applyTexts(name); here listener is null. share the code from where (your parent Fragment) u set your listener

    – Solaiman Hossain
    Mar 28 at 5:26

















  • listener.applyTexts(name); here listener is null. share the code from where (your parent Fragment) u set your listener

    – Solaiman Hossain
    Mar 28 at 5:26
















listener.applyTexts(name); here listener is null. share the code from where (your parent Fragment) u set your listener

– Solaiman Hossain
Mar 28 at 5:26





listener.applyTexts(name); here listener is null. share the code from where (your parent Fragment) u set your listener

– Solaiman Hossain
Mar 28 at 5:26












1 Answer
1






active

oldest

votes


















0















Solution: To pass data from an alert dialog to a parent fragment, the easier way is let the dialog extends DialogFragment. Then using setTargetFragment and setTargetFragment to send data between them.



Because you do not post parent fragment code, so I assume here is xml and java code of your parent fragment.



MainFragment.java



public class MainFragment extends Fragment implements SchulfachDialogListener

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_main, container, false);

Button btnShowAlertDialog = view.findViewById(R.id.button_show_alert_dialog);
btnShowAlertDialog.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
SchulfachDialog dialog = new SchulfachDialog();
dialog.setTargetFragment(MainFragment.this, 0);
dialog.show(requireActivity().getSupportFragmentManager(), null);

);

return view;


@Override
public void applyTexts(String text)
Toast.makeText(requireActivity(), text, Toast.LENGTH_SHORT).show();




fragment_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"
android:id="@+id/button_show_alert_dialog"/>

</LinearLayout>


Let the alert dialog extends DialogFragment.



public class SchulfachDialog extends DialogFragment 

private EditText editTextName;

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
View view = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_schulfach, null);

builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)


)
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String name = editTextName.getText().toString();
SchulfachDialogListener listener = (SchulfachDialogListener) getTargetFragment();
listener.applyTexts(name);

);
editTextName = view.findViewById(R.id.edit_name);

return builder.create();







share|improve this answer



























  • Works fine, thank you a lot !

    – Enigma
    Apr 11 at 18:36










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384916%2fpass-information-from-alertdialog-to-parent-fragment%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















Solution: To pass data from an alert dialog to a parent fragment, the easier way is let the dialog extends DialogFragment. Then using setTargetFragment and setTargetFragment to send data between them.



Because you do not post parent fragment code, so I assume here is xml and java code of your parent fragment.



MainFragment.java



public class MainFragment extends Fragment implements SchulfachDialogListener

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_main, container, false);

Button btnShowAlertDialog = view.findViewById(R.id.button_show_alert_dialog);
btnShowAlertDialog.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
SchulfachDialog dialog = new SchulfachDialog();
dialog.setTargetFragment(MainFragment.this, 0);
dialog.show(requireActivity().getSupportFragmentManager(), null);

);

return view;


@Override
public void applyTexts(String text)
Toast.makeText(requireActivity(), text, Toast.LENGTH_SHORT).show();




fragment_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"
android:id="@+id/button_show_alert_dialog"/>

</LinearLayout>


Let the alert dialog extends DialogFragment.



public class SchulfachDialog extends DialogFragment 

private EditText editTextName;

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
View view = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_schulfach, null);

builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)


)
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String name = editTextName.getText().toString();
SchulfachDialogListener listener = (SchulfachDialogListener) getTargetFragment();
listener.applyTexts(name);

);
editTextName = view.findViewById(R.id.edit_name);

return builder.create();







share|improve this answer



























  • Works fine, thank you a lot !

    – Enigma
    Apr 11 at 18:36















0















Solution: To pass data from an alert dialog to a parent fragment, the easier way is let the dialog extends DialogFragment. Then using setTargetFragment and setTargetFragment to send data between them.



Because you do not post parent fragment code, so I assume here is xml and java code of your parent fragment.



MainFragment.java



public class MainFragment extends Fragment implements SchulfachDialogListener

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_main, container, false);

Button btnShowAlertDialog = view.findViewById(R.id.button_show_alert_dialog);
btnShowAlertDialog.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
SchulfachDialog dialog = new SchulfachDialog();
dialog.setTargetFragment(MainFragment.this, 0);
dialog.show(requireActivity().getSupportFragmentManager(), null);

);

return view;


@Override
public void applyTexts(String text)
Toast.makeText(requireActivity(), text, Toast.LENGTH_SHORT).show();




fragment_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"
android:id="@+id/button_show_alert_dialog"/>

</LinearLayout>


Let the alert dialog extends DialogFragment.



public class SchulfachDialog extends DialogFragment 

private EditText editTextName;

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
View view = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_schulfach, null);

builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)


)
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String name = editTextName.getText().toString();
SchulfachDialogListener listener = (SchulfachDialogListener) getTargetFragment();
listener.applyTexts(name);

);
editTextName = view.findViewById(R.id.edit_name);

return builder.create();







share|improve this answer



























  • Works fine, thank you a lot !

    – Enigma
    Apr 11 at 18:36













0














0










0









Solution: To pass data from an alert dialog to a parent fragment, the easier way is let the dialog extends DialogFragment. Then using setTargetFragment and setTargetFragment to send data between them.



Because you do not post parent fragment code, so I assume here is xml and java code of your parent fragment.



MainFragment.java



public class MainFragment extends Fragment implements SchulfachDialogListener

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_main, container, false);

Button btnShowAlertDialog = view.findViewById(R.id.button_show_alert_dialog);
btnShowAlertDialog.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
SchulfachDialog dialog = new SchulfachDialog();
dialog.setTargetFragment(MainFragment.this, 0);
dialog.show(requireActivity().getSupportFragmentManager(), null);

);

return view;


@Override
public void applyTexts(String text)
Toast.makeText(requireActivity(), text, Toast.LENGTH_SHORT).show();




fragment_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"
android:id="@+id/button_show_alert_dialog"/>

</LinearLayout>


Let the alert dialog extends DialogFragment.



public class SchulfachDialog extends DialogFragment 

private EditText editTextName;

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
View view = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_schulfach, null);

builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)


)
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String name = editTextName.getText().toString();
SchulfachDialogListener listener = (SchulfachDialogListener) getTargetFragment();
listener.applyTexts(name);

);
editTextName = view.findViewById(R.id.edit_name);

return builder.create();







share|improve this answer















Solution: To pass data from an alert dialog to a parent fragment, the easier way is let the dialog extends DialogFragment. Then using setTargetFragment and setTargetFragment to send data between them.



Because you do not post parent fragment code, so I assume here is xml and java code of your parent fragment.



MainFragment.java



public class MainFragment extends Fragment implements SchulfachDialogListener

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_main, container, false);

Button btnShowAlertDialog = view.findViewById(R.id.button_show_alert_dialog);
btnShowAlertDialog.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
SchulfachDialog dialog = new SchulfachDialog();
dialog.setTargetFragment(MainFragment.this, 0);
dialog.show(requireActivity().getSupportFragmentManager(), null);

);

return view;


@Override
public void applyTexts(String text)
Toast.makeText(requireActivity(), text, Toast.LENGTH_SHORT).show();




fragment_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"
android:id="@+id/button_show_alert_dialog"/>

</LinearLayout>


Let the alert dialog extends DialogFragment.



public class SchulfachDialog extends DialogFragment 

private EditText editTextName;

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
View view = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_schulfach, null);

builder.setView(view)
.setTitle("Add new subject")
.setMessage("Message")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)


)
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String name = editTextName.getText().toString();
SchulfachDialogListener listener = (SchulfachDialogListener) getTargetFragment();
listener.applyTexts(name);

);
editTextName = view.findViewById(R.id.edit_name);

return builder.create();








share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 9:57

























answered Mar 28 at 9:43









Nhất GiangNhất Giang

5,4083 gold badges11 silver badges31 bronze badges




5,4083 gold badges11 silver badges31 bronze badges















  • Works fine, thank you a lot !

    – Enigma
    Apr 11 at 18:36

















  • Works fine, thank you a lot !

    – Enigma
    Apr 11 at 18:36
















Works fine, thank you a lot !

– Enigma
Apr 11 at 18:36





Works fine, thank you a lot !

– Enigma
Apr 11 at 18:36








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384916%2fpass-information-from-alertdialog-to-parent-fragment%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현