4

First, I'd to state that I've been searching for a solution for this problem for three days now, that may means either I'm not asking the right question or not using a good approach. If any, please guide me in the right direction.

This is the scenario: I've an Activity and a bound Service. The Service holds some data and processes it as necessary while posting a persistent (ongoing) notification with some information. The Activity has three Fragments inside a ViewPager that displays the data processed by the Service.

The Fragments are a List Fragment, that shows the active data entries available, a Details Fragment that displays the details for each data and a Parameters Fragment where the user can modify how the data is processed.

[Service] <-> ([Activity] -> [ViewPager([List], [Details], [Parameters])])

Everything works just fine. My Activity binds to the Service, the ViewPager is created after and then the Fragments fetch information trough an Interface.

Here comes the fun part... Screen Rotation!

As the Service binds asynchronously, when the user rotates the screen the Fragments no longer have the data because the Activity is bounding the service while they're already present and not recreated thanks to the ViewPager.

I've been trying to figure this out but it seems that I don't have the knowledge to solve it. I've tried making static references to the fragments, setting them up before the service is rebound but I can't get a stable solution.

I'd be using android:configChanges in my manifest but there are different layouts for each orientation.

Again, if I'm using a bad approach, please, guide me!

  • 1
    You can try using a headless fragment to hold your data and bind your service to. [Here](http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html) is a good tutorial. It talks about using headless fragment with async task but the same could be applied to your case. This is the recommended way of handling screen rotation. – Naveed Dec 18 '13 at 16:05
  • Just did, as @Kuffs also suggested, and it works flawlessly! Can't thank you enough guys! :) – Santiago G. Marín Dec 18 '13 at 19:03
  • Anyway you can show your code, I am trying to figure out how to implement an interface to a fragment in a viewpager from an activity – Lion789 Mar 07 '14 at 03:18
  • @Lion789 Sorry for my late answer. If you still need it I can send you the files via Dropbox. Let me know. – Santiago G. Marín Apr 09 '14 at 14:24

2 Answers2

2

Difficult to suggest when I don't know your code but thinking out loud....

Can you have a "worker fragment" that is never displayed (i.e headless) and has setRetainInstance(true) set so it does not lose any state you have set.

Your worker fragment would bind to the service instead of the activity and maintain a reference to it.

If you need to communicate with your Activity, you can do this with callbacks.

Your other fragments could communicate with the worker instead of the Activity.

This process would basically make the activity little more than a shell into which the rest of your components are hosted. Rotation would lose nothing because all data is held in the retained fragment.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
0

During the screen rotation process the activity is completely destroyed and use of android:congfigChange is discouraged. but what you can do is you can override saveInstanceState(bundle) method in which you can save the data present in your activity at the time it is destroyed by the system in response to the screen rotation. and later receive it as the system passes the bundle to the activities onCreate(bundle) method or get it from the restoreInstanceState(Bundle) method.

Niraj Adhikari
  • 1,678
  • 2
  • 14
  • 28