Thursday, July 25, 2013

Contd 1: How to build an Android application, step by step


Implementing application functionality

We've talked about how each activity has its own user interface, defined within a separate layout resource file. You might be wondering about implementation hurdles such as the following:
  • How do I control application state?
  • How do I save settings?
  • How do I launch a specific activity?
With our theoretical game application in mind, it is time to dive into the implementation details of developing an Android application. A good place to start is the application context.

Using the application context

The application context is the central location for all top-level application functionality. You use the application context to access settings and resources shared across multiple activity instances.
You can retrieve the application context for the current process by using the getApplicationContext() method, like this:
Context context = getApplicationContext();
Because the Activity class is derived from the Context class, you can use this instead of retrieving the application context explicitly.
You might be tempted to just use your Activity context in all cases. Doing so can lead to memory leaks, though. The subtleties of why this happens are beyond the scope of this article, but there is a great official Android blog post on this topic.
Once you have retrieved a valid application context, you can use it to access application-wide features and services.

Retrieving Application Resources

You can retrieve application resources by using the getResources()method of the application context. The most straightforward way to retrieve a resource is by using its unique resource identifier, as defined in the automatically generated R.java class. The following example retrieves a String instance from the application resources by its resource ID:
String greeting = getResources().getString(R.string.hello);

Accessing Application Preferences

You can retrieve shared application preferences by using thegetSharedPreferences() method of the application context. You can use the SharedPreferences class to save simple application data, such as configuration settings. Each SharedPreferences object can be given a name, allowing you to organize preferences into categories or store preferences all together in one large set.
For example, you might want to keep track of each user's name and some simple game state information, such as whether the user has credits left to play. The following code creates a set of shared preferences called GamePrefs and saves a few such preferences:
SharedPreferences settings = getSharedPreferences("GamePrefs", MODE_PRIVATE);

SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("UserName", "Spunky");
prefEditor.putBoolean("HasCredits", true);
prefEditor.commit();
To retrieve preference settings, you simply retrieve SharedPreferences and read the values back out:
SharedPreferences settings = getSharedPreferences("GamePrefs", MODE_PRIVATE);

String userName = settings.getString("UserName", "Chippy Jr. (Default)");

Accessing other application functionality using contexts

The application context provides access to a number of top-level application features. Here are a few more things you can do with the application context:
  • Launch Activity instances
  • Retrieve assets packaged with the application
  • Request a system-level service provider (for example, location service)
  • Manage private application files, directories and databases
  • Inspect and enforce application permissions
The first item on this list - launching Activity instances - is perhaps the most common reason you will use the application context.

Working with Activities

The Activity class is central to every Android application. Much of the time, you'll define and implement an activity for each screen in your application.
In the Chippy's Revenge game application, you have to implement five different Activity classes. In the course of playing the game, the user transitions from one activity to the next, interacting with the layout controls of each activity.

Launching Activities

There are a number of ways to launch an activity, including the following:
  • Designating a launch activity in the manifest file
  • Launching an activity using the application context
  • Launching a child activity from a parent activity for a result

Designating a launch activity in the manifest file

Each Android application must designate a default activity within the Android manifest file. In the manifest file of a Droid1 project, DroidActivity might be designated as the default activity.
Other Activity classes might be designated to launch under specific circumstances. You manage these secondary entry points by configuring the Android manifest file with custom filters.
In Chippy's Revenge, SplashActivity would be the most logical activity to launch by default.

Launching activities using the application context

The most common way to launch an activity is to use the startActivity() method of the application context. This method takes one parameter, called an intent. We will talk more about the intent in a moment, but for now, let's look at a simple startActivity() call.
The following code calls the startActivity() method with an explicit intent:
startActivity(new Intent(getApplicationContext(), MenuActivity.class));
This intent requests the launch of the target activity, named MenuActivity, by its class. This class must be implemented elsewhere within the package.
Because the MenuActivity class is defined within this application's package, it must be registered as an activity within the Android manifest file. In fact, you could use this method to launch every activity in your theoretical game application; however, this is just one way to launch an activity.

Launching an Activity for a Result

Sometimes an activity wants to launch a related activity and get the result, instead of launching an entirely independent activity. In this case, you can use the Activity.startActivityForResult() method. The result will be returned in the Intent parameter of the calling activity'sonActivityResult() method. We will talk more about how to pass data using an Intent parameter in a moment. Next: Managing activity state

No comments:

Post a Comment

Kindly share your view or contribution on this topic