0

I'm trying to make it so a clear button will open up an alert dialog which has a yes or no. When clicking the yes button, it should pass a bool value from the dialog frag to the other frag. If the value is true, which it should be when yes is clicked, it will call methods which will clear a database. Here is the dialog frag and the part of the frag where I'm trying to implement it. I can't get the dialog box to appear, but so far it does make the screen darker which I assume means I'm not hooking it up right.

Dialog frag:

public class DialogClear extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.clear_dialog, null);
        final Button yes = dialogView.findViewById(R.id.yes);
        final Button no = dialogView.findViewById(R.id.no);

        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        return builder.create();
    }

}

Here is how I'm trying to call it from my frag

       clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogClear = new DialogClear();
                dialogClear.setTargetFragment(BloodPressureFragment.this, 1);
                dialogClear.show(getFragmentManager(),"");

                    dataManager.clearDatabase();
                    dataManager.createDatabase();
                    dataText.setText("");
                    dataText.setBackgroundResource(R.drawable.no_border);
                    updateList();
                }

        });
Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • Does this answer your question? [How to send data from DialogFragment to a Fragment?](https://stackoverflow.com/questions/18579590/how-to-send-data-from-dialogfragment-to-a-fragment) – Shashanth Nov 16 '19 at 05:13
  • @Shashanth So do I create the interface inside the Dialog frag? – DoctorArnold Nov 16 '19 at 05:48
  • Interface file you can create anywhere inside the project. You have to implement that interface inside caller activity/fragment. When you done with the job in your dialog fragment, pass the required value to the implementing method. – Shashanth Nov 16 '19 at 05:55
  • ViewModel's can easily solve this problem and it will retain data even on Orientation change. – Anmol Nov 16 '19 at 08:11

2 Answers2

0

welcome to code party

you have many choices to do this job, but i will show you best way for using fragments.

As you know, all fragments changes in one activity and extend from it.

So the easy way is this:

1.build public fields in your activity

2.build a getInstant method on activity

3.fill and get values of that activity from any fragments that you want show on this

see this example:

public boolean isLocationOn;

this field is build in activity now: in any fragment:

MainActivity.getInstance().isLocationOn = true;

in other fragment:

if(MainActivity.getInstance().isLocationOn){

 //todo show map or ...
}

in this way you can use anything in fragments

update me in comments

alireza daryani
  • 787
  • 5
  • 16
  • 1
    how will you retain the data if orientation is changed? – Anmol Nov 16 '19 at 08:12
  • when orientation change, onViewCreate called in fragment and u can do this job and calling in there – alireza daryani Nov 16 '19 at 13:41
  • Activity instance will die and data attached to it will also get destroyed. You can save ur primitive/serialised data in bundle `onSaveInstanceState` but still the complex data still need's to be persisted in a DB or in android file system.So to solve all of the problem's and to keep the state of the application alive ViewModel's are best solution. – Anmol Nov 16 '19 at 13:51
  • when orientation changed, view will create again and data in activity never lose.watch the lifecycle of activity with fragments. – alireza daryani Nov 17 '19 at 04:47
0

You should read and try using 3 thing's to solve this.

1. Navigation Component

  • easy to navigate to fragment's and DialogFragment

2. View Model

  • Share same View Model between different fragment's of an activity
  • Data is not lost on Orientation change's
  • All business logic at one place and easy to unit test

3. Live Data

  • recommended for responsive ui
  • Easy to understand Api's
Anmol
  • 8,110
  • 9
  • 38
  • 63