0

Im using a MainActivity with a few Fragments.

In the Activity I connect to a server.

I used: android:configChanges="orientation|screenSize" in my Manifest.

So that the Activity keeps the connection on orientation change.

But now I cant use different layout for port/land (Fragments).

Is there a way to force the Fragments to reload on change without the Activity reloading?

TeKo
  • 465
  • 1
  • 5
  • 17

1 Answers1

0

You need to move your network operations outside of the Activity/Fragment lifecycle so that your Activity and Fragments can work as intended and you can have long running network operations that aren't interrupted by Activity and Fragment lifecycles. Remember that Activities and Fragments are meant to be dynamic, have narrow concerns, etc. They're not designed for network operations. That's why there's SO much emphasis on using background threads for various things and why there are Services. For example, you could use an IntentService to handle your network operations. Here's an example of how to your Activity would deal with resuming/updating information when configuration changes happen: https://stackoverflow.com/a/4481297/2306657

Community
  • 1
  • 1
KMDev
  • 615
  • 6
  • 10
  • Didnt know about Services... The server connection was still working after the recreation because it was running in a seperate thread but I wasnt able to control anything. Thanks, reading the dev pages about Services now. – TeKo Aug 20 '13 at 00:33
  • I read the pages about IntentService and people are saying that I should not use a loop in the Service. Im connecting to a Server and have to be connected even if Im not changing anything, so the service should start when I use a Switch in my Activity and end when I use the switch again. While the Service (Connection to the Server) is running I have to communicate via Fragments. – TeKo Aug 24 '13 at 16:34