Reputation: 1994
I'm creating an app that is a specialized timer. I'm showing a countdown timer along with doing some other things during the countdown. Having come from a MVVM background I wanted to use the new Android binding framework for this project. I setup my view as in the example:
<data>
<variable
name="viewModel"
type="com.examples.viewmodels.MainActivityVM"/>
</data>
With a TextView to show the countdown.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@{viewModel.timeLeftText}"
android:textAlignment="center"
android:textSize="144dp"/>
And the class for the activity I've setup the binding in the onCreate override:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setViewModel(this.viewModel);
}
Everything works as advertised. I can click a button on the view and start my countdown. The business logic class I wrote handles updating the view model instance with the time left and the view updates as I would expect. Great!
However, I'm running into an issue where if the device is rotated from portrait to landscape the view completely looses its connection to the view model. What's odd is that it goes back to its default start state as if the app has just started for the first time.
It occurred to me that there is a case where the app could pause and then resume based on changes. I verified it by looking on the Google and found that an app could pause and resume when the orientation changed. I changed the activity in the manifest to be this:
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:label="@string/app_name">
...
</activity>
This didn't help. What am I missing? (probably a lot!)
Upvotes: 2
Views: 2836
Reputation: 15414
Firstly, Android activity is always destroyed and recreated when its orientation changes. In your words What's odd is that it goes back to its default start state as if the app has just started for the first time
. There is nothing odd about it, in fact its clearly mentioned here.
If you want the Android system to not recreate the activity, you could use
android:configChanges="orientation|screenSize"
The reason your code doesnt work is because probably you're targetting API level 13+ as mentioned here.
Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
Upvotes: 1