Книга: Beginning Android
DIY Rotation
DIY Rotation
Even this, though, may still 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 upon the device’s position as determined by accelerometers.
The third possibility for handling rotations, therefore, is to tell Android that you will handle them completely yourself and that you do not want assistance from the framework. To do this:
1. Put an android:configChanges
entry in your AndroidManifest.xml
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 activity-destruction process and simply get a callback letting you know of the change.
To see this in action, turn to the Rotation/RotationThree
sample application. Once again, our layouts are the same, so the application looks the same as the preceding two samples. However, the Java code is significantly different, because we are no longer concerned with saving our state, but rather with updating our UI to deal with the layout.
But first, we need to make a small change to our manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.commonsware.android.rotation.three"
android:versionCode="1"
android:versionName="1.0.0">
<application android:label="@string/app_name">
<activity android:name=".RotationThreeDemo"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here, we state that we will handle keyboardHidden
and orientation
configuration changes ourselves. This covers us for any cause of the “rotation” — whether it is a sliding keyboard or a physical rotation. Note that this is set on the activity, not the application — if you have several activities, you will need to decide for each which of the tactics outlined in this chapter you wish to use.
The Java code for this project follows:
public class RotationThreeDemo extends Activity {
static final int PICK_REQUEST = 1337;
Button viewButton = null;
Uri contact = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupViews();
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode==PICK_REQUEST) {
if (resultCode==RESULT_OK) {
contact = data.getData();
viewButton.setEnabled(true);
}
}
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setupViews();
}
private void setupViews() {
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.pick);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_PICK,
Uri.parse("content://contacts/people"));
startActivityForResult(i, PICK_REQUEST);
}
});
viewButton = (Button)findViewById(R.id.view);
viewButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, contact));
}
});
viewButton.setEnabled(contact!=null);
}
}
The onCreate()
implementation delegates most of its logic to a setupViews()
method, which loads the layout and sets up the buttons. The reason this logic was broken out into its own method is because it is also called from onConfigurationChanged()
.
- CHAPTER 26 Handling Rotation
- Советы по использованию Мастера фунций
- VMware Distributed Resource Scheduler (DRS)
- В.4. Небольшие тестовые программы
- Корпоративная книга продаж
- Глава 3. Искусство интрапренерства
- Ввод данных о предприятии
- Тема 11. Проектирование структуры организации
- Отношения с людьми
- Сопутствующие функции
- Этот изменчивый мир