Android - Activity destroyed when rotating the tablet

Asked

Viewed 407 times

5

I have the following problem in my application:

  • When the user turns off the screen on the tablet, turn the screen again, rotates to the vertical position and logs in android again, a Activity user was destroyed and back to the application login screen.

  • This problem only occurs in Activity which have a list of Adapter has an XML that contains a value property using @dimen.xml.

Example:

View row = LayoutInflater.from(mContext).inflate(R.layout.custom_attachment_list_item,
                parent, false);

My XML:

<?xml version="1.0" encoding="utf-8"?>
<TextView android:layout_width="20dp"
    android:padding="@dimen/sp_gen_xl_15dp"
    android:text="@dimen/sp_box_margin_left"
    android:layout_height="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android" />

That is, if I use android:padding="@dimen/sp_gen_xl_15dp" to define the padding, to Activity is destroyed. If I pass any value "10dp" for example, the Activity is maintained when the user performs the procedure described above.

Obs. Activity reported already has in the Manifest the tag android:configChanges="orientation|screenSize that serves not to destroy the Activity when the tablet rotate.

Example:

<activity
     Activity"
            android:keepScreenOn="true"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:windowSoftInputMode="adjustNothing"
            android:configChanges="orientation|screenSize"></activity>

The big question is: Why the tag orientation|screenSize placed in the Manifest only works in Activitys that nay utilize @dimen in their XML?

1 answer

0

To understand what is happening, it is necessary to remember some points:

  • You can have different xmls depending on the screen configuration. For example, you can have an xml for when Activity is in Landscape and another for when you are in Portrait. In the same way you can have different folders for different pictures with different sizes, you can have for xmls. For example, you can have the meuLayout.xml in the layout folder and the meuLayout.xml in the folder /res/layout-land, where the latter will be used, automatically, when the orientation of the mobile phone changes to Landscape.

  • It is in the instance of Context that all these variables are. There are three implementations of Context: Acitivty, Service and your app process.

When the screen rotates, android has to re-contextualize, as you are referencing an attribute defined in a resource file, it will always have to reinstall the Context, which in this case is the activty.

Usually the android:configChanges should only be used as a last resort. The ideal is to use Fragments with the method setRetainInstance(true), Since Fragments are not Context implementations, they will not have this activity behavior.

  • I noticed you are releasing this error: Binary XML file line #3: Binary XML file line #3: You must Supply a layout_height attribute. Caused by: java.lang.Unsupportedoperationexception: Binary XML file line #3: You must Supply a layout_height attribute

  • It occurs when I turn the tablet and it tries to inflate in XML again. The impression I got is that the attribute lost the value that came from @dimen, that is, this empty.

Browser other questions tagged

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