0

I have a tabbed activity that shows a fragment by a viewpager, as usual. This fragment have a list.

One of the actions of the user shows a dialogfragment to user insert a new item in this list.

I show the dialogfragment with edittexts to user create a new item.

The question is: how can I insert this item on the viewpagers' fragment list?

From any fragment I can call getActivity() to access the activity, but how access another fragment that is being shown behind the dialogfragment?

Thanks in advance.

Informatheus
  • 1,025
  • 1
  • 9
  • 20

3 Answers3

1

It sounds like you want to get the results from the dialogfragment (what the user has inserted on the dialogfragment edit-texts) and use this in the fragment that called the dialogfragment (to add as new item to the list) - in that case, the selected answer here solves this problem - also I think this Gist is a good resource to reference. In your case, I also think implementing some sort of a custom listener/callback as they did in this Gist is a good idea. Hope this helps.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
1

Fragment with the List items - FragmentA Dialog - NewItemDialogFragment

The method you're missing is setTargetFragment(). While building your NewItemDialogFragment, invoke this method passing the FragmentA as the target fragment for your dialog. Later, you can access the FragmentA instance by calling getTargetFragment() inside NewItemDialogFragment and cast it to the FragmentA and add newly created item.

Alternatively, you can create the contract interface between the FragmentA and the NewItemDialogFragment

dawid gdanski
  • 2,432
  • 3
  • 21
  • 29
1

You can use event bus for this.

http://square.github.io/otto/

This is an example of usage:

Bus bus = new Bus();

bus.post(new AnswerAvailableEvent(42));

@Subscribe public void answerAvailable(AnswerAvailableEvent event) {
    // TODO: React to the event somehow!
}

bus.register(this); // In order to receive events, a class instance needs to register with the bus.
Bogdan Ustyak
  • 5,639
  • 2
  • 21
  • 16