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

Asked

Viewed 83 times

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 shifts from vertical to landscape print everything back on Logcat, does anyone know why this happens? Usage System.out.println.

  • 1

    He must be entering again in the Oncreate method, post the screen that is with this problem.

2 answers

3

To be able to develop proficiently on Android you need to understand the lifecycles ( Life Cycles ) of the elements. This is a basic feature of the system and dominating it is fundamental.

When you rotate your device the system manager destroys its activity as well as all fragments and elements, saves the current state ( basically the visual features) and reconstructs everything from scratch. For this reason your Logcats are always displayed again.

That means in this process you will lose any reference that has not been properly saved. I recommend that you study lifecycles, especially Activities and Fragments, because they are slightly different. It follows a diagram of Lifecycle of an activity for you to have an idea.

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

Good luck =)

inserir a descrição da imagem aqui

2

To resolve this problem open the Androidmanifest.xml file, then in your Activity statement 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.