0

I have an activity containing bottom navigation and 3 fragments. I use the repository pattern, lets say its all about user object. Currently, each fragment has its own presenter that i’m instantiating in the onCreateView. I attach the view in onCreateView and detach it in onDestroyView. If I navigate to another fragment ,rotate the screen or the process dies, the presenters will be just recreated for each scenario and I will get the user object from the repository again (it is cached and therefore no heavy operations each time). Now for the problematic scenario:

User clicks to upload a photo -> the photo is being uploaded and the view presents a loading indicator->user navigates out of the fragment->view is detached-> the presenter unsubscribes for the upload complete listener of the repository ->user navigates back to the first fragment -> a new presenter is created and attached -> he neither sees any photo nor an uploading indicator (the uploading is still in progress but there are no subscribers anymore)

Now there are few options:

  1. I am missing something
  2. I am missing something very basic
  3. I should keep an active listener to catch any change(currently using single listener)
  4. I should assume success when uploading an image but then I will not know about the failure

What am I missing?

Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • If you get any solution, on regarding this issue "Implementing android MVP pattern and an activity with multiple fragments". Thanks – Morshed Alam Nov 09 '18 at 07:42

1 Answers1

0

There are at least a few things you can do here.

  1. You can use retained fragments that store your presenter. When you are loading a fragment you can check if it is retained and use that one or create a new one. (I am not an expert at this).

  2. You can create a separate object/manager that manages the pending tasks/photo uploads. When the screen loads, you can have your presenter check for outstanding tasks and indicate that when the user returns to the view.

  3. You can attach the listeners/subscribers to the activity or a different object that lives outside of the fragment lifecycle and query that when your presenter loads.

There are probably a few other ways to do this, but those are the first three that came to mind.

dazza5000
  • 7,075
  • 9
  • 44
  • 89