Выбрать главу

    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));

   }

  });

  restoreMe(savedInstanceState);

  viewButton.setEnabled(contact!=null);

 }

 @Override protected void onActivityResult(int requestCode, int resultCode,

  Intent data) {

  if (requestCode==PICK_REQUEST) {

   if (resultCode==RESULT_OK) {

    contact = data.getData();

    viewButton.setEnabled(true);

   }

  }

 }

 @Override

 protected void onSaveInstanceState(Bundle outState) {

  super.onSaveInstanceState(outState);

  if (contact!=null) {

   outState.putString("contact", contact.toString());

  }

 }

 private void restoreMe(Bundle state) {

  contact = null;

  if (state!=null) {

   String contactUri = state.getString("contact");

   if (contactUri!=null) {

    contact = Uri.parse(contactUri);

   }

  }

 }

}

By and large, it looks like a normal activity… because it is. Initially, the “model” — a Uri named contact — is null. It is set as the result of spawning the ACTION_PICK sub-activity. Its string representation is saved in onSaveInstanceState() and restored in restoreMe() (called from onCreate()). If the contact is not null, the “View” button is enabled and can be used to view the chosen contact.

Visually, it looks like Figures 26-1 and 26-2.

Figure 26-1. The RotationOne application, in portrait mode

Figure 26-2. The RotationOne application, in landscape mode

The benefit to this implementation is that it handles a number of system events beyond mere rotation, such as being closed by Android due to low memory.

For fun, comment out the restoreMe() call in onCreate() and try running the application. You will see that the application “forgets” a contact selected in one orientation when you rotate the emulator or device.

Now With More Savings!

The problem with onSaveInstanceState() is that you are limited to a Bundle. That’s because this callback is also used in cases where your whole process might be terminated (e.g., low memory), so the data to be saved has to be something that can be serialized and has no dependencies upon your running process.

For some activities, that limitation is not a problem. For others, though, it is more annoying. Take an online chat, for example. You have no means of storing a socket in a Bundle, so by default, you will have to drop your connection to the chat server and re-establish it. That not only may be a performance hit, but it might also affect the chat itself, such as you appearing in the chat logs as disconnecting and reconnecting.

One way to get past this is to use onRetainNonConfigurationInstance() instead of onSaveInstanceState() for “light” changes like a rotation. Your activity’s onRetainNonConfigurationInstance() callback can return an Object, which you can retrieve later via getLastNonConfigurationInstance(). The Object can be just about anything you want — typically, it will be some kind of “context” object holding activity state, such as running threads, open sockets, and the like. Your activity’s onCreate() can call getLastNonConfigurationInstance() — if you get a non-null response, you now have your sockets and threads and whatnot. The biggest limitation is that you do not want to put in the saved context anything that might reference a resource that will get swapped out, such as a Drawable loaded from a resource.

Let’s take a look at the Rotation/RotationTwo sample project, which uses this approach to handling rotations. The layouts, and hence the visual appearance, is the same as with Rotation/RotationOne. Where things differ slightly is in the Java code:

public class RotationTwoDemo extends Activity {

 static final int PICK_REQUEST = 1337;

 Button viewButton = null;

 Uri contact = null;

 @Override

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  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));

   }