Development Resource Project
Nice n' Easy JQuery Image Rotator
Getting Set up with Ogre 3D on Ubuntu
Scrollable Tables with Floating Header using CSS
Book Review: How to Implement Design Patterns in PHP
Changing Mailman Python Scripts for Virtual Host Support

Adding a Second Activity to your Android App

Wednesday, 28 September 11, 1:00 pm
Android apps are organised into distinct 'activities', each represented by an instance of Activity or one of its subclasses.

When you create a new Android project in Eclipse, a default Activity is created for you. For anything beyond the most simple app, a single Activity will not be enough. The user can easily switch back and forth between different activities, and they are the basic way of subdividing distinct functionality in Android.

Here I'm going to go over the process of adding a second Activity to a project. In Eclipse, the best way is to start by creating the required entry in the application's AndroidManifest.xml file.
  1. Open the AndroidManifest.xml file by double-clicking it
  2. Switch to the Application tab at the bottom of the editor
  3. Click the Add.. button in the Application Nodes section
  4. Make sure Activity is selected in the pop-up dialog and click OK
  5. A new section will now appear to the right of Application Nodes, called Attributes for Activity.
  6. Click the underlined link Name in this section and the New Java Class wizard opens, with the superclass and package prefilled. If you want to extend a class other than Activity, e.g. ListActivity, you can change that here.
  7. Enter a name for your new Activity, and also check the box to create stubs for 'Inherited abstract methods'

The Eclipse editor will then open showing the newly generated class code that you can edit at your leisure.

Switching to Your App's Second Activity

If you add a button to your app's main layout file with the ID switcherbtn, the following code is how you would set an onClick() listener to make that button switch to the other activity you created above:
public class MainActivity extends Activity { private Button btnSwitcher;   public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);   // Get hold of your switcher button btnSwitcher = (Button) findViewById(R.id.switcherbtn);   // Set an onClick listener so the button will switch to the other activity btnSwitcher.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Switch to Other Activity Intent myIntent = new Intent(v.getContext(), OtherActivity.class); startActivityForResult(myIntent, 0); } }); } }

Passing Objects Between Two Activities

Simple primitive data types such as ints, floats, and Strings can be passed via 'extras', by calling the appropriate putExtra() method on the Intent instance you're using to switch activities. You retrieve such extras using the corresponding getExtra() methods.

For more complex data, such as a custom object, or an ArrayList of custom objects, you have a few options:
  1. wrap the object in a class that implements the Parcelable interface, which can be stored in an extra
  2. wrap the object in a class that implements the Serializable interface, which can be stored in an extra
  3. use static data members to store objects that need to be accessed by multiple activities
  4. use external storage (file, database, SharedPreferences)
  5. use a common component, such as a custom Application or a local Service
In general, parceling or serialising an object in order to share it between activities involves copying the data and thus is wasteful of resources, meaning that these two approaches are often not the best solution when sharing large data structures.

Please enter your comment in the box below. Comments will be moderated before going live. Thanks for your feedback!

Cancel Post

/xkcd/ Earthquake Prediction Flowchart