Refresh Fragment from AlertDialog - android

I have a Fragment with a ListView on it. This ListView is filled with an adapter.
I start a DialogFragment an there I can type in the data for a now row on the ListView.
Now I want to refresh the Fragment under the Dialog when the Dialog is closed.
How can I refresh the ListView? I can't call the notifyDataSetChanged() from outside the fragment.
I read something about a callback loader, but I populate the ListView with an array. As far as I understand the callback Loader i have to use a Cursor for that.
I also tried to call a function of the activity but then i couldn't call the notifyDataSetChanged() too. To make the Adapter static didn't work.
I Hope someone understands my Problem and can halp to fix it.

In your case I would pass the Adapter instance to the class, which creates the Dialog. I don't see there any other solution.
You can also create the Dialog in the Fragment class and define the OnClickListener as inner class, so you can directly access the Adapter.
You can also use something like an event bus, which helps a lot for louse coupling.

You could just set the calling fragment as listener.
FragmentManager fm = getSupportFragmentManager();
MyDialog d = new MyDialog();
d.setDataChangeListener(this);
d.show(fm, "fragment_name");

Related

How to update my Fragment List From my Activity?

I would like to change dynamically my List of Fragment which will be sent to an adapter in Parent Activity.
I have tried to send it by a Bundle but the RecyclerView is not refreshed.
I also have tried to implement a method in my Fragment to be called from my Activity.
What's the best way the do that?
Search for observer pattern
https://www.baeldung.com/java-observer-pattern
The idea is making your fragment like an observer for any updates

Is it a good practice to start a fragment from an adapter?

I have an activity with fragment A. Fragment A hosts a list in recyclerview which requires an adapter. When an item from the list is clicked I want to open another fragment, say B, showing additional details about the item.
I can open fragment B in three ways:
From the recyclerview adapter itself where I will have the item position etc.
From fragment A using callback from the adapter, since the adapter has all the required info like position, object etc.
From the activity, again using callback. If i do from the activity, i will have to add callback interface from adapter to fragment A, and finally to the activity. Looks too much.
I want to know what is the best way to open fragment B.
Hey its not good practice to start the fragment from adaptor. because it will be very complex to find the container of fragment. so please always try to start the fragment from main activity which will be parent of all fragment.I hope its help you.
Option 2 is always good approach->
2. From fragment, A using callback from the adapter, since the adapter has all the required info like position, object etc.
"According to MVC pattern adapter is always used for binding view with lists. So adapter should always independent from the fragment so the Single Responsibility Principle will always be handled. So, there should be no dependency from the adapter to fragment but fragment to the adapter. When you call fragment or activity from adapter it will create a cyclic dependency with one another so memory will not clear until you finish the apps. You can call any callback method of the fragment from the adapter which method will call the desired fragment you want."

How to make DialogFragment call a method inside another Fragment

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.

Fragment with a ListView calling another Fragment

I have an Fragment with a ListView from where i want to call another Fragment. In my BaseAdapter i want to register an onClick and open a new Fragment where i use a ViewPager.
Is here Broadcastreciver/sender the right way to go? or can i just call the new Fragment in the onClick method?
If Broadcastreciver ist the way to go, i would appreciate it if you could help me with the Code. I have never used a Broadcastreciver before. What comes in the onClick method and what needs to be done in my mainActivity.
Thanks

"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.

Categories

Resources