DialogFragment Lifecycle Fragment and Relationship
I have Fragment
"A" where I have ImageButton
. Pressing this button brings up DialogFragment
"B" to the foreground , where Fragment
"A" is partially displayed in the background. DialogFragment
"B" presents the user with a list of options. When you press a certain selection, DialogFragment
"B" is rejected with Dismiss()
and Fragment
"A" becomes fully visible again.
During this action, I need to update ImageButton
to Fragment
"A" to represent the user's choice of DialogFragment
"B" (basically a new image for ImageButton
).
Am I correct in thinking the right place to update ImageButton
on chunk "A" at time OnResume
? Is "A" OnPause
displayed FragmentDialog
on b while "B" is displayed ? Therefore, returning from DialogFragment
"B", Fragment
"A" will call it OnResume
and what is there, where should I make the update changes to ImageButton
be presented to the user?
Hope my explanation is clear. Any detailed help on where and how I should update ImageButton
is highly appreciated.
source to share
Fragment A will not go into onPause so onResume will not be called http://developer.android.com/guide/components/fragments.html
Resumed The
fragment is displayed in the current job.Suspended
Another activity is in the foreground and has the focus, but the activity in which he lives, this fragment is still visible (foreground activity is partially transparent or does not cover the entire screen).
source to share
I had the same problem when using the method of reverse-dial interface, but OnResume
of Fragment
no fire, when DialogFragment
was sacked, because we do not switch to another activity.
So, this is where Event Bus made life easier. Event Bus is the easiest and most efficient way to communicate between actions and fragments in just three steps, you can see it here
This is nothing but a mechanism for publishing events / signatures. You will get proper documentation here
Add EventBus dependency to your gradle file - compile 'org.greenrobot:eventbus:x.x.x'
OR compile 'org.greenrobot:eventbus:3.1.1'
(Specific version)
For the above scenario -
Create one custom POJO class for custom events -
public class UserEvent {
public final int userId;
public UserEvent(int userId) {
this.userId = userId;
}
}
Sign an event Fragment A
whenever it is posted / posted from DialogFragment
or elsewhere -
@Subscribe(threadMode = ThreadMode.MAIN)
public void onUserEvent(UserEvent event) {
// Do something with userId
Toast.makeText(getActivity(), event.userId, Toast.LENGTH_SHORT).show();
}
Register or unregister your EventBus from your lifecycle Fragment A
, especially in onStart
and onStop
accordingly -
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
At the end, by clicking on a specific element, Post / Submit your event from DialogFragment
-
EventBus.getDefault().post(new MessageEvent(user.getId()));
source to share