SkeletonActivitypublic class SkeletonActivity extends android.app.Activity This class provides a basic demonstration of how to write an Android
activity. Inside of its window, it places a single view: an EditText that
displays and edits some internal text. |
Fields Summary |
---|
private static final int | BACK_ID | private static final int | CLEAR_ID | private android.widget.EditText | mEditor | android.view.View.OnClickListener | mBackListenerA call-back for when the user presses the back button. | android.view.View.OnClickListener | mClearListenerA call-back for when the user presses the clear button. |
Constructors Summary |
---|
public SkeletonActivity()
|
Methods Summary |
---|
public void | onCreate(android.os.Bundle icicle)Called with the activity is first created.
super.onCreate(icicle);
// Inflate our UI from its XML layout description.
setContentView(R.layout.skeleton_activity);
// Find the text editor view inside the layout, because we
// want to do various programmatic things with it.
mEditor = (EditText) findViewById(R.id.editor);
// Hook up button presses to the appropriate event handler.
((Button) findViewById(R.id.back)).setOnClickListener(mBackListener);
((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener);
// There are two cases we are interested in here: If icicle
// is supplied, we need to place it into the text editor to
// put us back to that state. Otherwise, we need to initialize
// the text editor to its default state.
Bundle textState = icicle != null ? icicle.getBundle("text") : null;
// Note that this example is a little different than what is common
// in a "real" application, where the data being edited is stored
// in some persistent back-end content provider. Here we save and
// restore all of the text editor state, including the text itself.
// If the text was kept in persistent storage, we would instead load
// the text from storage and restoreState() would only restore
// the normal editor state (cursor position etc).
// If we have state, try to restore it; if we don't have state
// or the restore fails, initialize to default state.
if (textState == null || !mEditor.restoreState(textState)) {
mEditor.setText(getText(R.string.main_label));
}
| public boolean | onCreateOptionsMenu(android.view.Menu menu)Called when your activity's options menu needs to be created.
super.onCreateOptionsMenu(menu);
// We are going to create two menus. Note that we assign them
// unique integer IDs, labels from our string resources, and
// given them shortcuts.
menu.add(0, BACK_ID, R.string.back).
setShortcut(KeyEvent.KEYCODE_0, 0, KeyEvent.KEYCODE_B);
menu.add(0, CLEAR_ID, R.string.clear).
setShortcut(KeyEvent.KEYCODE_1, 0, KeyEvent.KEYCODE_C);
return true;
| protected void | onFreeze(android.os.Bundle outState)Called to remember the current state before the activity stops.
super.onFreeze(outState);
// Save the current text editor state. As noted above in
// onCreate(), this is somewhat unusual in that our text
// is not in persistent storage. If we did have it in persistent
// storage, we would call mEditor.saveState() here instead.
outState.putBundle("text", mEditor.saveStateAndText());
| public boolean | onOptionsItemSelected(Menu.Item item)Called when a menu item is selected.
switch (item.getId()) {
case BACK_ID:
finish();
return true;
case CLEAR_ID:
mEditor.setText("");
return true;
}
return super.onOptionsItemSelected(item);
| public boolean | onPrepareOptionsMenu(android.view.Menu menu)Called right before your activity's option menu is displayed.
super.onPrepareOptionsMenu(menu);
// Before showing the menu, we need to decide whether the clear
// item is enabled depending on whether there is text to clear.
menu.setItemShown(CLEAR_ID, mEditor.getText().length() > 0);
return true;
| protected void | onResume()Called when the activity is about to start interacting with the user.
super.onResume();
|
|