1

I made a little map application with few activities using osmdroid and i run it on my android device but when i change the orientation of my device it seems that the application come back to the main activity.

For example i launch the application and i am looking for my current position in vertical(portrait) mode but if i change the orientation to the landscape mode my application restarts and i loose my current location finded in portrait mode...

I have no idea why it's doing that...

  • Do you want run your app in both `Portrait` and `Landscape` mode? – M D Apr 13 '14 at 11:12
  • 1
    Go this SO post [http://stackoverflow.com/questions/8093213/android-save-application-state-on-screen-orientation-change](http://stackoverflow.com/questions/8093213/android-save-application-state-on-screen-orientation-change) – M D Apr 13 '14 at 11:15

4 Answers4

3

When configurations such as screen orientation change, Android restarts the running activity. These events are called runtime changes. This is why you are losing your current location.

You need to save the data so that when something like that happens you can resume from where you were.

More information can be found on the Android Developer website.

ataulm
  • 15,195
  • 7
  • 50
  • 92
magmike
  • 473
  • 1
  • 5
  • 18
0

In your manifest, in your activities definition add android:configChanges="orientation". Example:

<activity
    android:name=".MainActivity"
    android:label="@string/title_activity_main"
    android:configChanges="orientation">

else add as well screenSize:

android:configChanges="orientation|screenSize"
Onik
  • 19,396
  • 14
  • 68
  • 91
ahmed_khan_89
  • 2,755
  • 26
  • 49
  • Are there any downsides to using this technique? Is it recommended, and if there _are_ any caveats, what else should be considered when employing this approach? – ataulm Apr 13 '14 at 13:04
  • this will just handle natively the changement of what your orientation. you don't have to add anything. For your data you can put them somewhere in safe, and they will not depend on the context of the activity but on the context of the application. In your place i would put my location in a singleton class. Now it begins too long for a coment... I posted a new answer for that – ahmed_khan_89 Apr 13 '14 at 16:33
  • 1
    really what I was hoping for was some reference to the fact that you'll have to then [handle layout changes yourself](http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange) (i.e. call `setContentView`). While this method _seems_ to solve OP's issue when rotating, it won't work when Android destroys and recreates your application (e.g. low system memory). By using this method, you essentially side-step the problem for a little while, without actually solving it. – ataulm Apr 13 '14 at 16:51
0

Override the method in your Activity class:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO: handle the new configuration
}
ataulm
  • 15,195
  • 7
  • 50
  • 92
Eng.amany
  • 25
  • 1
  • 5
  • This method won't get called unless you also state that you'll be taking responsibility for configuration changes by setting the `configChanges` attribute in AndroidManifest. – ataulm Apr 13 '14 at 16:54
0

If you want to share some data between all your activities; i mean all the application. Then you don't really care about the context of the activity ... you have many choices . See this answer ; it contains many some examples of code and a good explanation .

Community
  • 1
  • 1
ahmed_khan_89
  • 2,755
  • 26
  • 49