1
Maybe the problem is simple, but either way, I’m stalling to do it. I know I would have been able to do this through interfaces, but I don’t want it that way. My app has only one Activity
that houses many Fragments
. To Activity
implements the design standard Navigation Drawer
. She initially displays a Fragment
. Think of it this way:
Fragment 1
> Fragment 2
> Fragment 3
> Fragment 4
.
Most of Fragments
are lists. Now follow the sequence, I click on an item from Fragment 1
and go to the Fragment 2
, click on an item from Fragment 2
and go to the Fragment 3
. Now on the Fragment 3
, i press on an item, select an option (from a DialogFragment
and go to the Fragment 4
.
Man Fragment 4
is a screen that has data to be saved in the database and that some of them are displayed in the Fragment 3
. I have the option to edit this data and save, or just access the Fragment 4
and do nothing, giving up saving and returning to the Fragment 3
.
My problem is this: if I access the Fragment 4
and not save, returning to the Fragment 3
, nothing should happen, only the transaction goes back to normal anyway, ie as the transaction is in the Backstack
it will be reversed. Now, if I access the Fragment 4
and save the data, I must return to the Fragment 3
and update the interface. For example, I go to Fragment 4
, after saving the data, I give a popBackStack()
and go back to the Fragment 3
updating its interface, instead of going back to the Fragment 3
from where you left off that’s what happens.
I tried to do it with the method public void onBackStackChanged()
in the Fragment 3
but this method is called whenever any change occurs in the BackStack
, I mean, just to access the Fragment 3
from the Fragment 2
he is already called.
Lucas, if you already make the communication between the
Activity
and theFragments
pq doesn’t update this way? When saving the records in BD onFragment4
why not notify theActivity
to update theFragment3
? When it is removed (theFragment3
) I think he still getsAttached
, just give find by tag. Can you verify this hypothesis? Unfortunately I’m not around to test.– Wakim
@Wakim I wanted a much simpler way to do this. As for the
Fragment 3
even after it is removed and the transaction added to theBack Stack
, the instance ofFragment
it still exists. I could do it this way but I believe that it should have a way let’s say "practice" of doing it.– Lucas Santos
A simple way, but it requires a little initial work but then it is easy. Although I have never used I suggest using
EventBuses
, is a simplified form ofListeners
. Take a look at Libraries implementing this concept.– Wakim
Okay. I’ll do a little research and come back here with what I found on this.
– Lucas Santos
Get a look at these Ibraries: http://square.github.io/otto/, https://github.com/stickyd/eventbus and http://greenrobot.github.io/EventBus/.
– Wakim
@Wakim tried to do this instead of using external libraries. Place the method that updates the UI (E initializes in case it is first called) within the method
onActivityCreated()
. The problem with that is that I wear oneListView
withCustomAdapter
and the items are duplicated when I return fromBackStack
. I tried to set theListAdapter
to null always before updating the UI but did not help.– Lucas Santos
onActivityCreated is called more than once? Strange... Why not clean the Adapter (
clear
) and redo the load callingnotifyDataSetInvalidated
ornotifyDataSetChanged
?– Wakim
It’s not strange, it’s because he’s called when he’s created and when he comes back from
BackStack
also. I’ll try that you said. You happen to know how to useloaders
with custom Adapter?– Lucas Santos
I’ve never used
loaders
, always usedAsyncTask
and created my ownAdapter
extending the classBaseAdapter
. At first it’s just reload, but you could have a smart way of knowing whether or not you should (in case there’s a change in the comic book).– Wakim
I do the same as you but only for custom lists. Lists with standard items I use loaders because he already knows when to update or not. I also wondered if you could do the same with custom lists.
– Lucas Santos
Unfortunately I can not help, I am missing with this part of Loaders. It was nice to meet, I will give a study on it later.
– Wakim
@Lucassantos tried to do this validation on onResume? It will be called whenever Fragment interacts with the user.
– Neto Marin
@Netomarin am trying to implement a custom Loader. If I get warning here.
– Lucas Santos
@Netomarin managed to solve my problem.
– Lucas Santos