Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






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.


Date: 2014-12-29; view: 857


<== previous page | next page ==>
Sending an intent to other apps | Add it to the manifest
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)