Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an Object to Fragment or DialogFragment upon Instantiation

I'm trying to work out the correct way to pass in an Object to a Fragment or DialogFragment without breaking the 'empty constructor' rule.

For example I have created a custom View and for each one I instantiate I want to associate a DiaglogFragment. This DialogFragment will be used to display controls with which the user can alter certain aspects of the custom View it is associated with. Because View is an Object I understand I cannot use setArguments().

I could implement a newInstance(View) method of my DialogFragment i.e. Factory pattern but then what happens if my Fragment is saved by the system and then restored at a later date? As far as I can tell there will be no reference to the View object?

Could someone tell me if I am using Fragments in the wrong way or is there way to achieve passing in an object to the Fragment which will also cope with the system reconstructing it at a later time.

like image 878
D-Dᴙum Avatar asked May 06 '13 19:05

D-Dᴙum


People also ask

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

Is DialogFragment deprecated?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.

How send data from dialog to fragment in Android?

Then to show your dialogFragment your just call CurrencyDialogFragment(this). show(fragmentManager,"dialog") . this is the interface object you will be talking to, to pass data back to your Fragment.


1 Answers

In your DialogFragmnet class, you create two methods:

  1. newInstance to make instance of your DialogFragment

  2. setter to initialize your object

and add setRetainInstance(true); in onCreate

public class YourDialogFragment extends DialogFragment {

    ComplexVariable complexVar;

    public static YourDialogFragment newInstance(int arg, ComplexVariable complexVar) {
        YourDialogFragment frag = new MoveSongDialogFragment();
        Bundle args = new Bundle();
        args.putInt("count", arg);
        frag.setArguments(args);
        frag.setComplexVariable(complexVar);
        return frag;
    }

    public void setComplexVariable(ComplexVariable complexVar) {
        this.complexVar = complexVar;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }
}

then, to show the dialog

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();

Fragment prev = manager.findFragmentByTag("yourTag");
if (prev != null) {
    ft.remove(prev);
}

// Create and show the dialog.
DialogFragment newFragment = YourFragmentDialog.newInstance(argument, yourComplexObject);
newFragment.show(ft, "yourTag");
like image 183
Karim Aly Avatar answered Sep 20 '22 06:09

Karim Aly