I have two fragments, A and B let's say, where B contains a list. I would like to add a listener on Fragment B that notifies Fragment A of the chosen list item. I couldn't figure out how to initialize the listener in Fragment B since it is bad practice to pass arguments in fragment's constructors.
NOTE: Fragment B is contained inside Fragment A. i.e. I have a FrameLayout in Fragment A; and Fragment B covers that FrameLayout.
Any idea how I could do that?
If you're saying that Fragment B is a child fragment of Fragment A (that is, you've added it to Fragment A using Fragment A's getChildFragmentManager()), then you can use the same approach that you use for Activity interfaces, but using getParentFragment() instead of getActivity().
For example:
Fragment B:
#Override
public void onAttach(Context context) {
MyInterface myInterface = (MyInterface) getParentFragment();
}
Assuming that Fragment A implements MyInterface.
One convenience method we've used to avoid having to know whether a Fragment is hosted by another Fragment or an Activity is something like:
public static <T> getInterface(Class<T> interfaceClass, Fragment thisFragment) {
final Fragment parent = thisFragment.getParentFragment();
if (parent != null && interfaceClass.isAssignableFrom(parent)) {
return interfaceClass.cast(parent);
}
final Activity activity = thisFragment.getActivity();
if (activity != null && interfaceClass.isAssignableFrom(activity)) {
return interfaceClass.cast(activity);
}
return null;
}
Then you can just use:
MyInterface myInterface = getInterface(MyInterface.class, this);
and it doesn't matter whether Fragment B is hosted as a child Fragment or in an Activity directly.
A better approach for this situation, since what you want to do is communication between fragments, is to use an interface. You want to notify A when B has changed. This should be done through the parent activity. Here is the android documentation on the topic: https://developer.android.com/training/basics/fragments/communicating.html.
The gist of it is that you want to define an interface with a method called OnItemSelected (you can name it whatever you want). In B, you want a reference to this interface. When an item is selected, call your new OnItemSelected method. Implement this interface in the parent activity of the two fragments. In the implementation, you can put whatever code you want to modify A.
An example
CommunicationInterface
public interface CommunicationInterface {
public void onItemSelected(int position);
}
FragmentB
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
CommunicationInterface myInterface = (CommunicationInterface) getActivity();
// What ever else you want here
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
myInterface.onItemSelected(position);
}
MainActivity
public class MainActivity extends FragmentActivity implements CommunicationInterface {
// What ever other code you have
#Override
public void onItemSelected(int position) {
FragmentA fragA = (FragmentA)
getSupportFragmentManager().findFragmentById(R.id.fragment_a);
// Code to interact with Fragment A
}
Checkout the contract pattern https://gist.github.com/JakeWharton/2621173
If you are using multiple fragment, you dont have do it for every fragment, just add it to your BaseActivity if you have one.
This example shows the communication between activity and fragment. But for nested fragment you can replace the acitivy with getParentFragment();
Related
any android developer here? i need to ask something regarding communication between different fragment like i select something from list of fragment one and on that even fragment two is replaced with another layout?
i have listview adapter in fragment 1 and its view in fragment two.
in fragment one i have already implement onClickListener and switch by getting the position from clicklistener method. i used fragment manager but not getting the way i want.
please reply with code and explanation!
any help will be appreciated!
If I understand your question, what you're trying to implement is a "master-detail" flow. Let us call your first Fragment the list Fragment and your second Fragment the detail Fragment and let us assume that they both exist within a parent Activity.
To establish communication between your list Fragment and your detail Fragment, you could define a callback interface that the parent activity of both Fragments implement. So, in your list Fragment, you would have:
public class MyListFragment extends ListFragment {
// Create a reference to your callback
private ListItemSelectedListener mCallback;
// Define your callback interface
public interface ListItemSelectedListener {
onListItemSelected(int position, Object data);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the parent activity
mCallback.onListItemSelected(position, getData(position));
}
}
Then, your parent activity implements the callback listener and triggers a change in the detail Fragment when necessary.
public class MasterDetailActivity extends Activity
implements MyListFragment.ListItemSelectedListener {
//....
// Implement the callback method defined in the interface
public void onListItemSelected(int position, Object data) {
// Get a reference to your detail Fragment
DetailFragment detailFragment = (DetailFragment)
getFragmentManager().findFragmentById(R.id.detail_fragment);
/* This is assuming both the list fragment and the detail fragment
* exist within the parent Activity
*/
if (detailFragment != null) {
detailFragment.changeDetailView(position, data);
}
}
}
Check out Google's document on Fragment communication to learn more.
I have two static fragments in same activity, in "fragmentA" i have a customized list, when an item is clicked must to appear a detail in "fragmentB", detail appear only when i change screen orientation, no automatically. I use this code in main activity for refresh but application restart(detail appear).
finish();
startActivity(getIntent());
Someone knows a better way to make appear detail automatically in "fragmentB" when i clicked some item from "fragmentA", always using two static fragments in same activity.
Don't use static references to hold a Fragment, it's a really bad practice.
Don't store the Context in a static reference. Or you could will leak memory.
Instead, implement an Interface:
//FragmentActivityTest
public class FragmentActivityTest extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentB fragmentB = new FragmentB();
FragmentA fragmentA = new FragmentA();
fragmentA.setFragmentBHandler(fragmentB);
//Perform transactions etc
}
}
//FragmentA
public class FragmentA extends Fragment {
private FragmentBHandler _handler;
public void setFragmentBHandler(FragmentBHandler handler) {
_handler = handler;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((ListView) getView().findViewById(R.id.list_view)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
_handler.updateDetail();
}
});
}
}
//FragmentB
public class FragmentB extends Fragment implements FragmentBHandler {
#Override
public void updateDetail() {
//do your work
}
}
You should use an event bus like greenrobot or otto. FragmentB subscribe to an event, and FragmentA post that event. When you click on an item, you'll send an event, and the subscriber will execute your action (show details).
Without showing code, I can only guess your current implementation.
The proper way to communicate between fragments is
Pass data to the parent activity from Fragment A on item click,
Activity passes this data to fragment B by finding the fragment in fragment manager and call a method in fragment B,
That method in fragment B should determine if it should populate the detail.
How can I communicate a listview of a ListFragment after an event of a Fragment inside another Fragment?
In the ListFragment (fragment A) I have a method to refresh the ListView. But also, I need to refresh it after a click of a Button inside a Fragment, wich is child of another Fragment (fragment b)
Its like Fragment A (listFragment) | Fragment B (detailview)
(fragment C - child fragment of B)
How can I do it?
You can access another Fragment by its tag:
// find your fragment
YourFragment f = (YourFragment) getSupportFragmentManager().findFragmentByTag("yourFragTag");
// update the list view
f.updateListView();
The tag of your Fragment is set when it is attached to a container layout:
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frameBuy, YourFragment.newInstance(), "yourFragTag").commit();
So when you click your Button, find the Fragment you want to refresh by its tag, and then call your refresh method.
IF you are using a ViewPager, this is how to get the Fragments tag:
/**
* Gets the fragment tag of a fragment at a specific position in the viewpager.
*
* #param pos the pos
* #return the fragment tag
*/
public String getFragmentTag(int pos){
return "android:switcher:"+R.id.yourViewPagerId+":"+pos;
}
You can do it with a few simple steps:
Create a listener interface for component to listen to the button click event from FragmentC. For example:
public interface FragmentCButtonListener {
public void onButtonClicked();
}
Add a method in FragmentC to allow listener registration. FragmentC will keep track of the listener, and call the listener callback as soon as the button is clicked. For example:
public class FragmentC extends Fragment {
FragmentCButtonListener myListener;
public void registerListener (FragmentCButtonListener listener) {
myListener = listener;
}
}
FragmentA should implement FragmentCButtonListener interface, register itself as a listener to FragmentC, and refresh the list view when it receives the callback from FragmentC. For example:
public class FragmentC extends Fragment implements FragementCButtonListener {
FragmentC fragmentC;
public void onCreate() {
fragment = new FragmentC();
fragment.registerListener (this);
}
public void onButtonClicked() {
//refresh the list view
}
}
Please note, I assume the FragmentA has a reference to FragmentC in the sample. If not, just make sure the container class of all fragments registers itself as the listener of FragmentC. The container class can as FragmentA to update listview once it receives callback from FragmentC.
follow these steps
We have two fragments called AddFragmentand ListFragment, and upon adding an item on first fragment you want the updated list be shown on list fragment (what sort of sorcery is this!!!).
Step 1 create the listener interface on class level of AddFragment with a method that is going to be implemented by the other guy (ListFragment ) and create Interface type variable
public class AddFragment extends Fragment{
//listener varriable
//listener
public interface OnCategoryAddedListener{
public void refreshList();
}
private static OnCategoryAddedListener meAddListener;
}
Step 2 create register method on class level of the same AddFragment class and set listenter variable
public class AddFragment extends Fragment{
public void registerListener(OnCategoryAddedListener listener)
{
meAddListener = listener;
}
}
Step 3 upon any event cud be button click or yelling at ur application(that is considered rude event in android :-) ) check for listener object meAddListener variable and call the interface,
in a Shakespeare’s nutshell it means “for thou who implement ye interface and brought the method within ur class shelter, I shall give u my utmost privilege and blessing to …. ”
Step 4 On ListFragment implement the AddFragment’s interface,no turning back just go implement its method. Within that method u just call abracadabra to repopulate ur list or any sort of updatable android view object… and ur done
public class ListFragment extends Fragment implements AddFragment.OnCattegoryAddedListener{
//refer to AddFragment
AddFragment addFragment;
//once the fragment is within the memory scope u instantiate AddFragment
//and register listener with AddFragment context which inherently implement OnCategoryAddedListener
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categoryAddFragment = new CategoryAddFragment();
categoryAddFragment.registerListener(this);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
fillList();
}
public void fillList() {
ArrayAdapter<String> catArrayAdapter = new ArrayAdapter<>(context,android.R.layout.simple_list_item_1,getItems());
setListAdapter(catArrayAdapter);
}
//le interface method u can count on to refresh ur list content
public void refreshList(){
fillList();
}
check this out for a little more
enjoy the java magic
I have two fragments sitting side by side in the same activity. When I touch a button in the right fragment (fragment B), I need a TextView in the left fragment to update (fragment A). I have looked all over for the best way to do this, but nothing seems to work for my needs. Could someone possibly give me an example of how I would code this? Fragment A is set through the XML layout, and fragment B gets loaded programmatically into a container. I have tried accomplishing this by using a method in fragment A to update the text, and calling on that method from a method in the parent activity. I then call on the method in the parent activity from fragment B.
This is the code in fragment B that declares the interface and calls a method in the interface
AttackCards attackCards;
public interface AttackCards {
public void deckSize();
}
public void onAttach(DeckBuilder deckBuilder) {
super.onAttach(deckBuilder);
attackCards = (AttackCards) deckBuilder;
}
attackCards.deckSize(); //this is in my onclick methods
This is the code in the activity that implements the interface and calls the method in fragment A
public class DeckBuilder extends Activity implements AttackCards{
public void deckSize() {
DeckBuilderFragment deckBuilderFragment = (DeckBuilderFragment)getFragmentManager().
findFragmentById(R.id.deckbuilder_fragment);
deckBuilderFragment.deckSize();
}
This is the method in fragment A that appends the textview with the contents of a shared preferences value
public void deckSize() {
deckSize = (TextView) getView().findViewById(R.id.decksize);
final SharedPreferences defaultDeck = getActivity()
.getSharedPreferences("defaultDeck", Context.MODE_PRIVATE);
deckSize.setText(String.valueOf(defaultDeck.getInt("decksize", 0)));
}
Sadly this attempt simply brings me a nullpointer when touching a button. I am getting a null pointer at
attackCards.deckSize(); //this is in my onclick methods
Could someone please help me out with an example of how to do this correctly?
One fragment should not communicate to another fragment directly. It should do so through attached activity. The detail explanation with code example is available here
Android Developer site
Declare an interface in Fragment B, and implement the interface in the activity. Call the interface through callback in Fragment B when button is clicked. You can have a public function in Fragment A to update the TextView, so activity directly call the function to update the text.
You can define an interface in Fragment B and implement it on the MainActivity. Then on the callback method (onClickOnB in this case) set the text on the TextView. You should obtain a reference of the TextView in the Activity's onCreate() after setContentView(). This works because Fragment A is static. Otherwise, you can create a public method inside Fragment A so you can set the text from inside the callback by getting a reference of Fragment A and calling such method.
Fragment B
public class FragmentB extends Fragment implements onClickListener{
ClickOnB listener;
public void setOnFragmentBClickListener(ClickOnB listener){
this.listener = listener;
}
#Override
public void onClick(View v){
//stringMessage is a `String` you will pass to `Fragment` A to update its `TextView`
listener.onClickOnB(stringMessage);
}
interface ClickOnB{
public void onClickOnB(String message);
}
}
MainActivity
public class MainActivity extends Activity implements ClickOnB{
#Override
protected onCreate(Bundle savedInstanceState){
//Get a reference of `Fragment` B somewhere in your code after you added it dynamically and set the listener.
((FragmentB)getFragmentManager().findFragmentByTag("FragmentB")).setOnFragmentBClickListener(this);
}
#Override
public void onClickOnB(String message){
//Set the text to the `TextView` here (I am assuming you get a reference of the `TextView` in onCreate() after inflating your layout.
mTextView.setText(message);
}
}
I have this structure:
Activity A has a ViewPager. The pages of a ViewPager are as known Fragments.
Activity A has a list of complex objects, which I want to display in the pages.
When the user clicks on an item inside the ViewPager activity B must be started. Activity B also needs the list with complex objects.
So I have to pass this list
A -> Fragment(s) -> B
First I found out that it's not possible to use the fragment's constructor to pass data to it. I have to use parcelable or serializable. In the fragment I then have to parcel or serialize again to pass to B.
But I don't want to serialize or parcel whole list of complex data 2 times only to get it through to B.
I would like to avoid using static fields.
Then I came to the idea to pass a listener to the fragment, which listens to item click, and notifies my activity A and activity A then starts activity B with the data. This looks cleanest for me because A is actually the one the data belongs to, so A starts fragment with data, fragment comes back to A and then A starts B with data. I don't have to pass the data everywhere (serializing!).
But this doesn't seem to work, because how do I pass a listener to the fragment using serialization? What is wrong about using a listener in a fragment to come back to the activity?
How do I solve this?
Edit
I found this solution in a thread about dialog fragments: Get data back from a fragment dialog - best practices?
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
So I add this to my fragment and make my activity implement certain interface.
This would solve the problem of passing data from fragments to B. But I still need to pass the list from A to the fragments. Probably I'll have to use a Parcelable...
Edit: I tried Parcelable, it works. But the objects in my list have a lot of fields between them also maps, for example. Writing the Parcelable code (write / read) is a pain. Maybe I just stick to static data...
The listener solution you found is excellent and actually recommended by Google (http://developer.android.com/training/basics/fragments/communicating.html).
As for your remaining problem of passing data to fragments, are you sure you need the entire list passed, and not just an element of the list for each fragment in the ViewPager? If you just need an element, you can do something like this:
/*
* An adapter for swiping through the fragments we have.
*/
private class CustomPagerAdapter extends FragmentStatePagerAdapter {
List<Thing> things;
public CustomPagerAdapter(FragmentManager fragmentManager, List<Thing> things) {
super(fragmentManager);
this.things = things;
}
#Override
public int getCount() {
return things.size();
}
#Override
public Fragment getItem(int position) {
return ThingFragment.newInstance(things.get(position));
}
}
/*
* Loads a view based on the thing.
*/
private static class ThingFragment extends Fragment {
private String name;
static ThingFragment newInstance(Thing thing) {
ThingFragment f = new ThingFragment();
// add some arguments to our fragment for onCreateView
Bundle args = new Bundle();
args.putString("name", thing.getName());
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
name = getArguments().getString("name");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.thing_fragment_layout, container, false);
TextView t = (TextView)v.findViewById(R.id.thingText);
t.setText(name);
return v;
}
}
And pass your list to the adapter. Of course, this requires that your dynamics be created dynamically, but inflating xml fragments is really easy.
If you actually DO need to pass the entire list, yeah, you might have to make the individual complex object Parcelable, and then add it to your Fragment args like with Bundle.putParcelableArray().