- •Getting Started
- •Building Your First App
- •Dependencies and prerequisites
- •Creating an Android Project
- •This lesson teaches you to
- •You should also read
- •Create a Project with Eclipse
- •Create a Project with Command Line Tools
- •Running Your App
- •This lesson teaches you to
- •You should also read
- •Run on a Real Device
- •Run on the Emulator
- •Building a Simple User Interface
- •This lesson teaches you to
- •You should also read
- •Alternative Layouts
- •Create a Linear Layout
- •Add a Text Field
- •About resource objects
- •Add String Resources
- •Add a Button
- •Make the Input Box Fill in the Screen Width
- •Installing the sdk
- •Build an Intent
- •Sending an intent to other apps
- •Start the Second Activity
- •Create the Second Activity
- •Add the title string
- •Add it to the manifest
- •Receive the Intent
- •Display the Message
Start the Second Activity
To start an activity, call startActivity() and pass it your Intent. The system receives this call and starts an instance of the Activity specified by the Intent.
With this new code, the complete sendMessage() method that's invoked by the Send button now looks like this:
/** Called when the user clicks the Send button */ public void sendMessage(View view) { Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); }
Now you need to create the DisplayMessageActivity class in order for this to work.
Create the Second Activity
Figure 1. The new activity wizard in Eclipse.
To create a new activity using Eclipse:
Click New in the toolbar.
In the window that appears, open the Android folder and select Android Activity. Click Next.
Select BlankActivity and click Next.
Fill in the activity details:
Project: MyFirstApp
Activity Name: DisplayMessageActivity
Layout Name: activity_display_message
Title: My Message
Hierarchial Parent: com.example.myfirstapp.MainActivity
Navigation Type: None
Click Finish.
If you're using a different IDE or the command line tools, create a new file named DisplayMessageActivity.java in the project's src/ directory, next to the original MainActivity.java file.
Open the DisplayMessageActivity.java file. If you used Eclipse to create this activity:
The class already includes an implementation of the required onCreate() method.
There's also an implementation of the onCreateOptionsMenu() method, but you won't need it for this app so you can remove it.
There's also an implementation of onOptionsItemSelected() which handles the behavior for the action bar's Up behavior. Keep this one the way it is.
Because the ActionBar APIs are available only on HONEYCOMB (API level 11) and higher, you must add a condition around the getActionBar() method to check the current platform version. Additionally, you must add the @SuppressLint("NewApi") tag to the onCreate() method to avoid lint errors.
The DisplayMessageActivity class should now look like this:
public class DisplayMessageActivity extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); // Make sure we're running on Honeycomb or higher to use ActionBar APIs if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // Show the Up button in the action bar. getActionBar().setDisplayHomeAsUpEnabled(true); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } }
If you used an IDE other than Eclipse, update your DisplayMessageActivity class with the above code.
All subclasses of Activity must implement the onCreate() method. The system calls this when creating a new instance of the activity. This method is where you must define the activity layout with the setContentView() method and is where you should perform initial setup for the activity components.
Note: If you are using an IDE other than Eclipse, your project does not contain the activity_display_message layout that's requested by setContentView(). That's OK because you will update this method later and won't be using that layout.
