Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Building Your First App.doc
Скачиваний:
0
Добавлен:
01.04.2025
Размер:
484.35 Кб
Скачать

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:

  1. Click New in the toolbar.

  2. In the window that appears, open the Android folder and select Android Activity. Click Next.

  3. Select BlankActivity and click Next.

  4. 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.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]