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.
Related
I have an Activity with two Fragments. I decided to have one presenter for each "view". So 1 presenter for the main Activity, 1 for the first fragment, and 1 presenter for the second fragment.
I have uses-cases in which I don't know where which code goes.
The first is where to manage fragments with fragment manager ? Do I have to do calls like "beginTransaction().add" in the activity or in his presenter ?
The second is, when the user tap on a button in the activity, I've to do some things in the current fragment. Do I have to call the presenter of the activity which will call the fragment's method wanted, or directly in the method onClick in the activity I call this fragment's method?
PS: I don't want to use any lib/framework
The first is where to manage fragments with fragment manager ? Do I
have to do calls like "beginTransaction().add" in the activity or in
his presenter ?
Everything that is Android API related should be inside the Activity. So, beginTransaction() is a method of the FragmentManager and this is part of the Android API. The Activity is the contract between your app and the operating system. The presenter should not even know that it is used for an Android app. If you press a button inside the Activity for instance it goes like this:
Inside event handler method which is inside your Activity you do this:
Call activityPresenter.onButtonClicked() and it will call activityView.presentWhatTheButtonClickDid()
The second is, when the user tap on a button in the activity, I've to
do some things in the current fragment. Do I have to call the
presenter of the activity which will call the fragment's method
wanted, or directly in the method onClick in the activity I call this
fragment's method?
You would indirectly call the Fragment method via its presenter.
Inside event handler method which is inside your Activity:
Call activityPresenter.onButtonClicked() and it will call
fragmentPresenter.onButtonClicked() and it will calls
fragmentView.presentResult()
So, as you see the Activitiy's presenter needs to know the Fragment's presenter.
*You should not name your presenters with "activity" or "fragment" in its name to keep things abstract. I merely did this for simplicity.
Everywhere it is written that fragments have their own lifecycle . Also fragment life cycle depend on activity's lifecycle.What is the meaning of fragment's own lifecycle if it is dependent on activity's lifecycle?
First of all you need to understand what are lifecycle methods are and when are they called/invoked. Lifecycle methods are basically invoked at the different state of your Activty/Fragment. For example when you first launch your activity the following flow of events/methods are called depending upon the state of your activity. For example : When your activity is first launched OnCreate is called, when your activity is no longer visible then onStop is called. So basically you first need to learn at which state are these different activities called.Below is a great referential flowchart for the same.
Activity lifecycle methods :
Fragment lifecycle methods :
Now, when you create a fragment it is inflated into the activty. And it has its own set of lifecycle events/methods which are called and since the fragment is inflated into the activty when the state of your activity changes it effects the fragment and correspondingly different lifecycle methods of the fragments are called. Below is another pictorial representation of the relation between the lifecycle methods of the activity and the fragment.
Image source : Google Images
The activity lifecycle is fairly simple in comparison to the fragment lolcycle (image from Square's Advocating against Android Fragments)
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.
Is it possible that an Activity or Fragment object can be reused by Android system after onDestroy() is called?
In other words: is every call for onCreate() for Activity object and respectively onAttach() for Fragment object preceded by calling its constructor?
You shouldn't do that. Only framework should call methods like onCreate, onAttach etc..
After onDestroy activity should not be used!
ActvitiyLifecycle
Fragments
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.