Android Multiple Activities and Intents

Leave a Comment
For today, we’re going to work off of the same application code as last time (though the package / activity names have been renamed IntentActivities instead of FirstApp).
In Eclipse, expand the src folder in Package Explorer and right click on com.test.intentactivities –> New –> Class.  You will then be presented with the New Java Class window.  Some of the information (the Source Folder and Package) will already be taken care of leaving the Name and Superclass for us.  The Name here is relatively arbitrary.  The Superclass is not however.  If you remember our FirstAppActivity, it extended from Activity.  Setting our Superclass here will ensure that our new Activity also extends from Activity.
New Java Class
After clicking Finish, our new Activity will be added to our source code.  Opening it will show us a very empty new class:
public class ActivityTwo extends Activity {}

You need to override some base functionality. The method you want to override first is going to be onCreate so go ahead and select that method.  Again you have some base code that doesn’t do everything you need it to do:
   1:  @Override
   2:  protected void onCreate(Bundle savedInstanceState) {
   3:      // TODO Auto-generated method stub
   4:      super.onCreate(savedInstanceState);
   5:  }

The only thing you’re going to do with your second activity today is connect it to a Layout, however, you need to create that Layout first.  res -> layout ->right click -> New –> Other -> Android XML File. Enter a name (like “second_layout”) in the File textbox and click Finish.  After finishing, you’ll be taken to the graphical layout editor.  Again, you’re not going to get very fancy here so drag a TextView on to the layout from the Palette on the left.  By default your TextView’s id will be textView1 which is fine.  Your TextView on the other layout has a different id, however, it’s not an issue for different layouts to have views with the same id.  If, however, you have two views on the same layout with the same id, you will run into issues trying to get references to those views in your code.  Now that we’ve created a second layout, let’s go back to our ActivityTwo class.  After the call to super.onCreate you need to set thebcontentView to your new layout.  This is done via the following line of code:
setContentView(R.layout.second_layout);

Great, you’ve created a second activity and a layout and you’ve connected that layout to our Activity, but how do you actually show that on the screen?  To do this, you use Intents.  Intents are essentially messages that you can send between different parts of your application or to the Android OS itself.  To use our first intent, we must return to the IntentActivitiesActivity and the main.xml layout.  Go to the layout and drag another button onto the bottom beneath our “Click Me” button.  This should default to be id button2.  With this done, you can go to our code file, beneath where you get a reference for button1 and set it’s onClickListener you need to do the same for button2:
   1:  Button button2 = (Button) findViewById(R.id.button2);
   2:  button2.setOnClickListener(new OnClickListener() {            
   3:      public void onClick(View v) {                            
   4:      }
   5:  });

Now here’s where you get to the Intent.  There are assorted ways to setup an Intent and make it run.  You’re going to do the simplest way here:
   1:  button2.setOnClickListener(new OnClickListener() {            
   2:      public void onClick(View v) {
   3:          startActivity(new Intent(getApplicationContext(), ActivityTwo.class));
   4:      }
   5:  });

Here you’re instantiating an Intent object on the fly using the Application Context and your new Activity.  That gets passed into startActivity which does exactly as named.  You’re nearly ready to run now.  The last thing you need to do is make a small modification to your AndroidManifest file.  The manifest file needs to know about all the activities your application is going to run so near the bottom, between the end of the first activity and the end of the application you need to add an element for your new activity:
<activity android:name=".ActivityTwo" android:label="@string/app_name"/>

Now you can run your application.  Right Click on the application in Package Explorer and go to Run As –> Android Application or hit Ctrl+F11:


So this is your IntentActivities layout with the new button at the bottom.  Tapping that button should cause our newActivity to fire and give you this:
One thing I would like to highlight is what happens when you tap the Back button on the second activity.  Due to the way you’re starting ActivityTwo, if you tap Back, you will return to our first Activity.  You could think of yourActivities as a stack of cards.  When your app starts, card 1 is put down.  When you start your second activity, card 2 is put down on top of card 1.  When you hit the back button, card 2 is taken off to reveal card 1.  If you wanted to get rid of card 1 when you show card 2, or prevent the user from backing into the first activity, you could call finish immediately after starting your second activity like so:
   1:  public void onClick(View v) {
   2:      startActivity(new Intent(getApplicationContext(), ActivityTwo.class));
   3:      finish();
   4:  }
This will end your first activity and start your second activity.

0 comments:

Post a Comment