TAILIEUCHUNG - Lập trình Androi part 28

Để thấy điều này trong hành động, chuyển sang các Rotation / ứng dụng mẫu RotationThree. Một lần nữa, bố cục của chúng tôi là như nhau, do đó, ứng dụng trông giống như hai mẫu trước đó. Tuy nhiên, các mã Java là đáng kể khác nhau, bởi vì chúng tôi không còn quan tâm đến tiết kiệm nhà nước của chúng tôi, mà là với việc cập nhật giao diện người dùng của chúng tôi để đối phó với bố trí | CHAPTER 19 Handling Rotation private void restoreMe contact null if getLastNonConfigurationInstance null contact Uri getLastNonConfigurationInstance In this case we override onRetainNonConfigurationInstance returning the actual Uri for our contact rather than a string representation of it. In turn restoreMe calls getLastNonConfigurationInstance and if it is not null we hold onto it as our contact and enable the View button. The advantage here is that we are passing around the Uri rather than a string representation. In this case that is not a big saving. But our state could be much more complicated including threads sockets and other things we cannot pack into a Bundle. However even this approach may be too intrusive to your application. Suppose for example you are creating a real-time game such as a first-person shooter. The hiccup your users experience as your activity is destroyed and re-created might be enough to get them shot which they may not appreciate. While this would be less of an issue on the T-Mobile G1 since a rotation requires sliding open the keyboard and therefore is unlikely to be done mid-game other devices might rotate based solely on the device s position as determined by accelerometers. For these situations you may want to tell Android that you will rotations yourself and you do not want any assistance from the framework as described next. DIY Rotation To handle rotations on your own do this 1. Put an android configChanges entry in your file listing the configuration changes you want to handle yourself versus allowing Android to handle for you. 2. Implement onConfigurationChanged in your Activity which will be called when one of the configuration changes you listed in android configChanges occurs. Now for any configuration change you want you can bypass the whole activitydestruction process and simply get a callback letting you know of the change. To see this in action turn to the Rotation RotationThree sample application.

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.