Alex
Alex

Reputation: 1486

getTargetFragment return Null

I have parent fragment and dialog fragment in my application. On click a button Parent Fragment opens Dialog Fragment. This is my ParentFragment Class.

public class ParentFragment extends Fragment {
........
........
    public class ButtonClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            Fragment prev = getFragmentManager().findFragmentByTag("dialog");
            if (prev != null) {
                ft.remove(prev);
            }
            MyDialogFragment newFragment = new MyDialogFragment();
            newFragment.setTargetFragment(ParentFragment.this, 0);
            newFragment.show(getFragmentManager(), "dialog");
        }
    }

}

This is MyDialogFragment Class

public class MyDialogFragment extends DialogFragment {
..........
public MyDialogFragment() {
            parentFragment = (ParentFragment)getTargetFragment();

        }
...........
...........
}

When getTargetFragment in MyDialogFragment Class always return null.

Upvotes: 1

Views: 3697

Answers (3)

Martin Y.
Martin Y.

Reputation: 671

setTargetFragment() and getTargetFragment, are not simple getters and setters, they replace calls like:

parentFragment.getFragmentManager().putFragment(args, "bla bla tag", (Fragment)parentfragment);

Which is behind setTargetFragment(), where args are the argument to be set to the dialog fragment.

and

getFragmentManager().getFragment(getArguments(), "bla bla tag");

Which is behind getTargetFragment(). So as you can see, this logic involves fragment lifecycles, not just setting and getting a variable.

So for short, if you want to get the target fragment, just do it in onCreateDialog() or in onCreate(), methods of the DialogFragment, the arguments are already set there, so getTargetFragment() should not be null. :)

Upvotes: 0

Ankit Aggarwal
Ankit Aggarwal

Reputation: 5375

check your sequence of commands

MyDialogFragment newFragment = new MyDialogFragment(); // line 1
newFragment.setTargetFragment(ParentFragment.this, 0); // line 2

here you are first creating a new MyDialogFragment object. Remember this command(line 1) will invoke the constructor of MyDialogFragment. After this you are setting the target fragment (line 2).

If you look at your dialogFragment,

public MyDialogFragment() {
            parentFragment = (ParentFragment)getTargetFragment();    
}

you are trying to access target fragment in the constructor. So the target fragment will always be null because you are asssigning it after the constructor is called in line 2. Try it access it from some other method or pass this as a parameter to the constructor(which is not a good practice).

Upvotes: 7

user3068659
user3068659

Reputation: 443

The problem is you don't have a reference to the ParentFragment since getTargetFragment() is null, but you can fetch the Fragment other ways try using the FragmentManager's getFragmentById() or getFragmentByTag() methods.

Upvotes: 0

Related Questions