Update Fragment UI after coming back from Backstack

Asked

Viewed 858 times

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 the Fragments pq doesn’t update this way? When saving the records in BD on Fragment4 why not notify the Activity to update the Fragment3? When it is removed (the Fragment3) I think he still gets Attached, just give find by tag. Can you verify this hypothesis? Unfortunately I’m not around to test.

  • @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 the Back Stack, the instance of Fragment 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.

  • 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 of Listeners. Take a look at Libraries implementing this concept.

  • Okay. I’ll do a little research and come back here with what I found on this.

  • Get a look at these Ibraries: http://square.github.io/otto/, https://github.com/stickyd/eventbus and http://greenrobot.github.io/EventBus/.

  • @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 one ListView with CustomAdapter and the items are duplicated when I return from BackStack. I tried to set the ListAdapter to null always before updating the UI but did not help.

  • onActivityCreated is called more than once? Strange... Why not clean the Adapter (clear) and redo the load calling notifyDataSetInvalidated or notifyDataSetChanged?

  • 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 use loaders with custom Adapter?

  • I’ve never used loaders, always used AsyncTask and created my own Adapter extending the class BaseAdapter. 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).

  • 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.

  • 1

    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.

  • @Lucassantos tried to do this validation on onResume? It will be called whenever Fragment interacts with the user.

  • @Netomarin am trying to implement a custom Loader. If I get warning here.

  • @Netomarin managed to solve my problem.

Show 9 more comments

1 answer

1


I solved my problem using Custom Loader for my Custom Adapter. The difficulty was only in implementing the Loader, but thanks to this implementation, it was not necessary to communicate a Fragment with another or with Activity. When I was in a Fragment and go back to the other Fragment through the popBackStack(), Loader simply reloads by updating the data.

If you want to understand a little more about Loaders see here.

  • 1

    Cool @Lucassantos! Really using Oader is always a good solution. Afterwards, it would be nice to create a gist or something to share the code or his idea at least :)

  • @Neto Marin know if I can ask a question here and answer it myself as if it were like a tutorial teaching how to use a custom Loader? Thanks for the tip.

  • Yes, it is even encouraged if you find the solution. See a post in English: http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/

  • @Neto Marin thank you. I will do as soon as possible.

  • @Netomarin created the question, see: http://answall.com/questions/22342/o-que-s%C3%A3o-loaders-how-to-implement-one-Download-to-load-from-database-se/22343#22343

Browser other questions tagged

You are not signed in. Login or sign up in order to post.