Why every time I turn my Android phone from vertical it runs again?

3

When I'm making an app I usually put some print messages to know what's going on, but I realized that when I'm running the app and it changes from vertical to landscape it prints all over again in LogCat, does anyone know why this happens? Use System.out.println.

    
asked by anonymous 16.07.2015 / 16:32

2 answers

3

In order to develop Android proficiently you need to understand the lifecycles (Elements of Life). This is a basic feature of the system and mastering it is key.

When you rotate your device the system manager destroys its activity as well as all the fragments and elements, saves the current state (basically the visual characteristics) and reconstructs everything from scratch. That's why your LogCats are always displayed again.

This means that in this process you will lose any referrals that have not been properly saved . I recommend that you study the lifecycles a lot, especially the Activities and Fragments, because they are slightly different. Follow a lifecycle diagram of an activity to get an idea.

I recommend that you read this document here . It's in English, but it's FUNDAMENTAL to understand the android system.

Good luck =)

    
16.07.2015 / 18:17
2

To solve this problem open the file AndroidManifest.xml, then in the declaration of your activity add the following line:

android:configChanges="orientation|screenSize"

Example:

<activity
    android:name="com.example.activity.MainActivity"
    android:configChanges="orientation|screenSize"
    android:label="@string/app_name" >
</activity>
    
11.09.2015 / 22:04