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.
Related
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."
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.
So I currently have an app that has 4 tabs (fragments). They are fragments A,B,C,D, in that order.
Fragment A is the first view opened (along with B because viewPager loads the view before and after the current view).
When I click a button in Fragment A, it sends Data back to MainActivity and then sends that data out to Fragments B and C.
However, this is where the issue comes into play. Since Fragment B was already called, the View isn't updated once I click the button and send the data over, but Fragment C is because the view wasn't called before.
Is there any way that I can remedy this?
You can do it a few ways right.
Just set the data to the fragment and have it update its views
Have all the fragments like B and C register themselves to recieve data from the MainActivity and when MainActivity gets it's data set you tell all the registered receivers of the new data
Recreate the fragment
Use an event bus and tell all subsribers of the new data and MainActivity, Fragment B would get notified of new data. Fragment C would get its data when created by MainActivity
I think this list is pretty endless tbh
The key here is the fragments need to fetch the data from the actvitiy aswell as be updated by the activity. In which case you need to break your UI update behaviour out of onCreateView and into its own updateUI() function. updateUI(MyData) can then be called from onCreateView and also called in a setMyData() on the fragment. Just make sure you check the isAdded flag in setMyData.
This pretty much says it all:
http://developer.android.com/training/basics/fragments/communicating.html
I used a simple fragment communicator that allows the activity to call the fragment, and the same for a fragment to talk to the activity.
You can change the views with the new data based on calling the method from within the activity. The way I do it is set the fragments in the activity then pass them into the page adapter this way I can call the methods within the fragment and implement the fragmentcommunicator interface on the fragments.
You can honestly even avoid the interface if you want, but if you are going to include the same method in all the fragments to talk to them it is easiest.
If you show code, I can show you a quick example.
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.
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.