How to make DialogFragment call a method inside another Fragment - android

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.

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

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.

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.

Related

Activity to RecyclerView Adapter Communication(Recyclerview in Fragment)

I have a Activity which contains a fragment with recyclerview.
The recyclerview has few editable fields and need to put validations on clicking a button on activity.
Please let me know the solution or any techniques to overcome this problem statement
use this (click here ) functionality for data sharing between Fragment to fragment , Fragment to Activity and Activity to Activity
1) in your onCreate declare the BUSand register it
Bus bus = new Bus();
bus.register(this);
2) send data or any action using
bus.post(<your data or any Item>);
3) don't forgot to unregister in your onestroy
bus.unregister(this)
You can get that fragment instance either by 'ID' or by tag (you would need to give a tag while adding this fragment).
Now you can call any public method of that fragment using its instance.
For Example (Using tag), if you want to call a public method 'show()' of FragmentA. Then while adding it to the activity give it a tag like this:
Fragment fragmentA = new FragmentA();
getFragmentManager().beginTransaction()
.add(R.id.fragment_container,fragmentA,"YOUR_TARGET_FRAGMENT_TAG").commit();
And to get its instance (on a button click or as required):
FragmentA fragmentA = getFragmentManager().findFragmentByTag("YOUR_TARGET_FRAGMENT_TAG");
fragmentA.show();
Declare in your fragment the RecyclerView as public
Declare in your activity the fragment as variable
in your activity on click listener get the recyclerview with
fragment.recyclerview

Dialogs handled from CustomAdapter

The doubt I have is conceptual.
Activity A holds Fragment B. Fragment B has a list view which is filled by a custom adapter. Each item from the adapter has a checkbox which onChecked true should display a AlertDialog to allow users to choose a item. My question is, should this interaction with the dialog ( and its different listeners (onClick, onCancel, onKey, etc) ) be handled by the holder class of each item, by the adapter class, the fragment or the activity?
I'd wrap your AlertDialog with DialogFragment instead of using pure AlertDialog, so it will handle dialog recreation on configuration change for you. Now, the interactions from this dialog should be passed back to activity I assume, for this I'd create an interface and make your activity to implement it. And lastly in the DialogFragment#onAttach cast your activity to this interface and hold this reference in some field, once user done any interactions, use this reference to safely pass data back to activity, or any other client, implementing this interface. Hope this makes sense.

How to make call backs between Fragments of the same Ativity

I have 4 tabs in an Activity.
Each of them is a Fragment. And every Fragment has a ListView.
So, if i change the ListView in Fragment, it must change the ListView in all other Fragments ie.., Tabs.
The problem i face is while creating the interface instance.
It takes it's own onClick() method.
In case i want a callback to the parent activity i could have done that by overriding onAttach. But how to make a callback to a Fragment?
From Developers site:
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
So, make a callback to the Activity which in turn makes a callback to other fragments??
Thank You
It's pretty simple,all You need is steps below:
1) From onClick method in your first fragment make a function call of activity:
((IYourActivityInterface) getActivty()).activityMethod();
2) In your activity find fragment by tag or id and run it's method:
public void activityMethod(){
Fragment tabFragment = getFragmentManager().findFragmentByTag("second_fragment");
// or Fragment tabFragment = getFragmentManager().findFragmentById(R.id.frag);
if (tabFragment!=null){
((IFragmentInterface) tabFragment).fragmentMethod();
}
}
Hope this is what you are looking for.)

"Refreshing" a fragment from the MainActivity

So, there is a button in a ListFragment. The onCLick method of the button is implemented in the MainActivity(not sure if it is a proper solution, but it is what it is). When I click the button the AlertDialog pops up and when I choose one of the dialog options it changes the dataset my fragment is working with.
The problem is when the AlertDialog disappears, my ListFragment is still displaying old data.
Is there any way to update my ListFragment from the MainActivity?
I've tried making certain ListFragment methods static so that they could be called from the main activity, but those methods use non-static fields, etc. and thus cannot be static.
You should be able to update ListFragments by calling notifyDataSetChanged() on it's adapter (assuming that your adapter derives from BaseAdapter or any of it's subclasses). The easiest way to do this would probably be set an an DialogInterface.OnDismissListener on your dialog.
myDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog){
myBaseAdapter.notifyDataSetChanged();
}
});
You can either keep the reference to the Adapter or get it directly from the ListFragment depending on your implementation.
So, I declared an adapter of my ListFragment fragment as static, as well as the I declared a list from which this adapter is being filled - as static.
From the main activity I do this:
ListFragment.item.add(mChosenFilePath);
ListFragment.fileList.notifyDataSetChanged();
where:
item - is a list that contains the elements that are to be displayed
mChosenFilePath - path of file that has been added into the item as a result of the dialog
fileList - is my adapter
Set a tag, or id for the fragment. You can then call a method directly on the fragment from the Activity:
Fragment myne = findFragmentByTag( "MyFragment" );
MyFragment target = (MyFragment) myne;
target.refresh(); // 'Refresh' method to be declared by MyFragment implementation
There are three possible solutions.
Listen for the click in your fragment instead of the Activity.
Set a listener on the cancel button of your dialog, and reload the fragment as needed.
Add your fragment with a tag, get it from the manager by that tag, and call the appropriate method.

Call fragment events from activity

So, I got the event in my fragment to pass to the activity, so how do I make it so the activity in turns, notifies fragment B to do something. I want to fragment B to populate a custom list when fragment A has a list item clicked on. So, it sends the event to the activity, now how do I get the activity to call events in fragment B?
One way to do it would be like this in your activity:
FragmentB fragmentB = (FragmentB)getFragmentManager().findFragmentById(R.id.fragmentBId);
fragmentB.performSomeTask();
This is of course assuming that you have a publicly accessibly method in FragmentB called performSomeTask();
Hope that helps!
The best practice is probably to create interfaces for both fragments and then have the activity implement the interfaces. You want to have good decoupling between fragments so that you can reuse them in other places.

Categories

Resources