// ******************************************************************* // // ** Chapter 3 Code Listings ** // // ** Professional Android 2 Application Development ** // // ** Reto Meier ** // // ** (c)2010 Wrox ** // // ******************************************************************* // // ** Resources *************************************************** // // ******************************************************************* // ** Listing 3-1: Simple values XML To Do List #FF0000FF 5px Item 1 Item 2 Item 3 3 2 1 // ******************************************************************* // ** Listing 3-2: Hello World layout // ******************************************************************* // ** Listing 3-3: Simple menu layout resource // ******************************************************************* // ** Listing 3-4: Using resources in a layout // ******************************************************************* // ** Listing 3-5: Activity definition for handling dynamic resource changes // ******************************************************************* // ** Listing 3-6: Handling configuration changes in code @Override public void onConfigurationChanged(Configuration _newConfig) { super.onConfigurationChanged(_newConfig); [ ... Update any UI based on resource values ... ] if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { [ ... React to different orientation ... ] } if (_newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) { [ ... React to changed keyboard visibility ... ] } } // ** The Application Class *************************************** // // ******************************************************************* // ** Listing 3-7: Skeleton application class // Appliction Class Extenstion import android.app.Application; import android.content.res.Configuration; public class MyApplication extends Application { private static MyApplication singleton; // Returns the application instance public static MyApplication getInstance() { return singleton; } @Override public final void onCreate() { super.onCreate(); singleton = this; } } // Manifest entry [... Manifest nodes ...] // Use in code MyObject value = MyApplication.getInstance().getGlobalStateValue(); MyApplication.getInstance().setGlobalStateValue(myObjectValue); // ******************************************************************* // ** Listing 3-8: Overriding the application life cycle handlers public class MyApplication extends Application { private static MyApplication singleton; // Returns the application instance public static MyApplication getInstance() { return singleton; } @Override public final void onCreate() { super.onCreate(); singleton = this; } @Override public final void onTerminate() { super.onTerminate(); } @Override public final void onLowMemory() { super.onLowMemory(); } @Override public final void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } } // ** The Activity Class ****************************************** // // ******************************************************************* // ** Listing 3-9: Activity skeleton code package com.paad.myapplication; import android.app.Activity; import android.os.Bundle; public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } } // ******************************************************************* // ** Listing 3-10: Activity layout in XML // ******************************************************************* // ** Listing 3-11: Main application Activity definition // ******************************************************************* // ** Listing 3-12: Activity state event handlers package com.paad.myapplication; import android.app.Activity; import android.os.Bundle; public class MyActivity extends Activity { // Called at the start of the full lifetime. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize activity. } // Called after onCreate has finished, use to restore UI state @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Restore UI state from the savedInstanceState. // This bundle has also been passed to onCreate. } // Called before subsequent visible lifetimes // for an activity process. @Override public void onRestart(){ super.onRestart(); // Load changes knowing that the activity has already // been visible within this process. } // Called at the start of the visible lifetime. @Override public void onStart(){ super.onStart(); // Apply any required UI change now that the Activity is visible. } // Called at the start of the active lifetime. @Override public void onResume(){ super.onResume(); // Resume any paused UI updates, threads, or processes required // by the activity but suspended when it was inactive. } // Called to save UI state changes at the // end of the active lifecycle. @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save UI state changes to the savedInstanceState. // This bundle will be passed to onCreate if the process is // killed and restarted. super.onSaveInstanceState(savedInstanceState); } // Called at the end of the active lifetime. @Override public void onPause(){ // Suspend UI updates, threads, or CPU intensive processes // that don’t need to be updated when the Activity isn’t // the active foreground activity. super.onPause(); } // Called at the end of the visible lifetime. @Override public void onStop(){ // Suspend remaining UI updates, threads, or processing // that aren’t required when the Activity isn’t visible. // Persist all edits or state changes // as after this call the process is likely to be killed. super.onStop(); } // Called at the end of the full lifetime. @Override public void onDestroy(){ // Clean up any resources including ending threads, // closing database connections etc. super.onDestroy(); } } // ******************************************************************* //