Every time I attach a fragment to my activity, I want to register it to receive events from the activity. This is easy, because I can override FragmentActivity#onAttachFragment(Fragment). When the fragment is removed from the activity, I want to unregister it from receiving events. I expected there to be a onDetachFragment event that I could use in a similar manner, but I'm not finding it.
Is there another way to accomplish what I'm trying to do? I'd like to keep the registering/unregistering in the activity, as opposed to moving it to a base fragment class (where I could just use onAttach/onDetach).
its better to use the onStart(), onStop() method from your fragment. Just cast getActivity() to your calling activity class.
Related
I am just asking for knowledge , can we call Interface from one activity to another activity
If it possible then can anyone share code with me?
You can't
Why?
Activity object is created by system. We all just call startActivity() & work in its lifecycle methods like onCreate(), onStart() etc.
If you really need it you can use fragment.
I have activity which contain 3 fragment.
when the activity onPaused the 3 fragment set a value to a variable.
I want to check that var in the onPause activity.
but the problem that onPause activity called first and then the fragment onPause called.
How to solve that ?
so i need to run a function when all onPause function finish running ?
thanks
You could define an interface in your fragment and make the activity implement that interface. Then define one method like variableWasSet() in that interface and call it in onPause() in your fragment after you set your variable. That's a recommended way for communication between fragments.
Or use an event bus system for communication between fragments and activities.
Like otto or EventBus.
I have a dashboard that is a fragment. Everytime I click a button, the dashboard is replaced by another fragment.
The click listener is implemented inside the dashboard fragment class. But I read somewhere that the better way to do it is to make the listeners inside the activity. Is it true? Why?
If yes, I can change it, i only have to copy the method in dashboard fragment to the activity, and make use of XML onClick feature.
I honestly can't think of a reason for declaring an onClick listener for a fragment in the activity.
First, fragments are suppose to be modular. Maybe you use it with this activity or that one. Putting the onClicks in the activity hardcodes a relationship between the two. Your activity is searching for the fragment, which isn't always there, and your fragment can't work except in that activity.
Second, where you declare your on click determines where it's implicit reference will be to. If you declare it in the activity, it can call activity functions, but It has no idea which fragment it came from. How does it reference fragment functions / data? Sure there's elaborate workarounds but why?
On the other hand, if you put it in the fragment, it can call the fragment functions. and it has the same life-cycle as the fragment (being attached to a fragment view), so the implicit reference isn't going to create a memory leak (by itself anyways). And if you want to call the activity, just use getActivity and cast it to your interface or subclass.
Every time I want to make my fragment call it's onStart() or onResume(), I do the the next :
getFragmentManager().beginTransaction().replace(R.id.containero1, new Massenger_frag(conversationID)).addToBackStack(null).commit();
is this good for the performance, is there another way to tell the fragment the new information arrived, and it must refresh its UI.
You can keep a reference to a "Massenger_frag" object that was used when doing the fragment replacement. You can also define a method "refresh" in your "Massenger_frag" class and call it from that instance variable. onStart()/onResume() from your fragment would also call the same refresh method to avoid code duplication.
What you are currently doing is not fast and adds a significant overhead as the new fragment is constructed every time and the old one gets deallocated.
the solution is simple: I just use BroadCast sender and receiver, and send the information throw Activity A to Activity B, the Activity B in its onReceive() call a function that refresh its content!
Use interface and create custom EventListeners. Check an example here
Is it possible to dynamically register a broadcast receiver in a fragment to listen for connectivity state changes? If so how? If not, what are some workarounds for fragments?
EDIT: To register a BroadcastReceiver you need a Context. Since fragments live within activities, probably the best way to get a Context is to just use getActivity(). However, as gnorsilva explains below, there are certain special cases to look out for.
user853583 suggestion is a good one, but if you need access to a context inside a fragment you should use getActivity().getApplicationContext()
You should avoid passing an activity as a context whenever possible as this can introduce memory leaks - some object will hold on to that activity after its onDestroy() has been called and it won't be garbage collected.
Having said that, there are cases when you do need to pass an activity as a context - eg: for list adapters
Two more things though:
because a fragment is attached and detached from an activity, some times getActivity() returns null - you can call it safely inside certain lifecycle methods where you know an activity is alive eg: onResume()
if your fragment does not retain its instance i.e. is destroyed on orientation change, be sure to unregister your receiver in your fragment, for eg inside onPause() or onDestroy()
As far as I can see there is no way to register a BroadcastReceiver in a fragment. What do you need that broadcast receiver for? A nice solution is the one mentioned here
You can register a broadcast receiver like this :
getActivity().registerReceiver(...