Android - Activity Destroyed When Rotating Tablet

2

I have the following problem in my application:

  • When the user shuts off the screen on the tablet, turns the screen back on, rotates to the vertical position, and logs on the android again, the Activity that the user was is destroyed and returns to the login screen of the application .

  • This problem only occurs in Activity which has a list whose Adapter has an XML that contains a property valued 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 set padding , Activity is destroyed. If I pass any value "10dp" for example, Activity holds when the user performs the procedure described above.

Note:% reported% already has in Manifest the Activity tag that serves not to destroy android:configChanges="orientation|screenSize when the tablet p>

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 tag orientation | screenSize placed in Manifest only works in Activitys do not use @dimen in your XML ?

    
asked by anonymous 22.03.2018 / 16:10

1 answer

0

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

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

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

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 reinstate the Context , which in this case is the activty. >

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

    
22.03.2018 / 20:12