Thursday, October 14, 2010

Child windows / activities in Android

I have just posted a blog regarding the subject Adding a menu to your Android application.

As a natural succession but still being a separate subject is the handling of sub Activities (child windows) in Android.

Picking up where we left of at the previous post with the code that handles the actual menu item clicks.

/** Called when a menu item in the menu is clicked. */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
  case R.id.menuabout:
   Toast.makeText(this, "Droidulus 1.0 by Kenneth Thorman", Toast.LENGTH_SHORT).show();
   return true;

  case R.id.menuoptions:
   Intent intent = new Intent(Droidulus.this, OptionsScreen.class);
   startActivityForResult(intent, OPTIONS_ACTIVITY_REQUEST_CODE);
   return true;
   
  // Generic catch all for all the other menu resources
  default:
   if (!item.hasSubMenu()) {
    return true;
   }
   break;
 }
 
 return false;
}


the 2 lines in the case R.id.menuoptions: are responsible for starting a new sub activity. The Intent class is a rather large class and the online documentation for this class can be found here Intent | Android Developers. But to quickly summarize the parameters basically means new Intent(parent, child).

The Android help documentation states the following with regards to starting new Activities.

"The startActivity(Intent)  method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), along with the integer identifier it originally supplied.

If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED."

The functionality that I am after in this case is a Preferences form where I can configure my application. To be able to display this My Preferences / Options Activity I need to have the needed resources. The options form looks like this (I guess it could use a bit of UI tweaking).



The very nature of this activity also means that I am interested in knowing when the user in question has changed the preferences. As mentioned above this is easy with this following method on the calling (parent) Activity.


/** Called when a sub/child activity/form/screen is exiting. */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
 super.onActivityResult(requestCode, resultCode, intent);
 // We have change the saved settings so we need to initialize the application again
 initilize();
 presentProblem();
}


This method allows me to re-initialize the parts of the application that is affected by the changed preferences.

No comments: