Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Android Application Development Cookbook
Android Application Development Cookbook

Android Application Development Cookbook: Over 100 recipes to help you solve the most common problems faced by Android Developers today , Second Edition

eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Android Application Development Cookbook

Chapter 1. Activities

This chapter covers the following recipes:

  • Declaring an activity
  • Starting a new activity with an intent object
  • Switching between activities
  • Passing data to another activity
  • Returning a result from an activity
  • Saving an activity's state
  • Storing persistent activity data
  • Understanding the activity lifecycle

Introduction

The Android SDK provides a powerful tool to program mobile devices, and the best way to master such a tool is to jump right in. Though you can read this book from beginning to end, as it is a cookbook, it is specifically designed to allow you to jump to specific tasks and get the results immediately.

Activities are the fundamental building block of most Android applications as the activity class provides the interface between the application and screen. Most Android applications will have at least one activity, if not several (but they are not required). A background service application will not necessarily require an activity if there is no user interface.

This chapter explains how to declare and launch activities within an application and how to manage several activities at once by sharing data between them, requesting results from them, and calling one activity from within another.

This chapter also briefly explores the intent object, which is often used in conjunction with activities. Intents can be used to transfer data between activities in your own application, as well as in external applications, such as those included with the Android operating system (a common example would be to use an intent to launch the default web browser).

Note

To begin developing Android applications, head over to the Android Studio page to download the new Android Studio IDE and the Android SDK bundle:

http://developer.android.com/sdk/index.html

Declaring an activity

Activities and other application components, such as services, are declared in the AndroidManifest XML file. Declaring an activity is how we tell the system about our activity and how it can be requested. For example, an application will usually indicate that at least one activity should be visible as a desktop icon and serve as the main entry point to the application.

Getting ready

Android Studio is the new tool used to develop Android applications, replacing the now-deprecated Eclipse ADT solution. Android Studio will be used for all the recipes shown in this book, so if you have not already installed it, visit the Android Studio website (the link has been provided earlier) to install the IDE and the SDK bundle.

How to do it...

For this first example, we'll guide you through creating a new project. Android Studio provides a Quick Start wizard, which makes the process extremely easy. Follow these steps to get started:

  1. Launch Android Studio, which brings up the Welcome to Android Studio dialog.
  2. Click on the Start a new Android Studio project option.
  3. Enter an application name; for this example, we have used DeclareAnActivity. Click on Next.
  4. On the Add an Activity to Mobile dialog, click on the Blank Activity button, and then click on Next.
  5. On the Target Android Devices dialog, chose Android 6.0 (API 23) as the minimum SDK (for this example, it really doesn't matter which API level you chose, as activities have existed since API level 1, but choosing the latest release is considered to be the best practice). Click on Next.
  6. Since we chose the Blank Activity option earlier, the Customize the Activity dialog is shown. You can leave the defaults as provided, but note the default activity name is MainActivity. Click on Finish.

After finishing the wizard, Android Studio will create the project files. For this recipe, the two files that we will examine are MainActivity.java (which corresponds to the activity name mentioned in Step 6) and AndroidManifest.xml.

If you take a look at the MainActivity.java file, you will realize that it's pretty basic. This is because we chose the Blank Activity option (in Step 4). Now look at the AndroidManifest.xml file. This is where we actually declare the activity. Within the <application> element is the <activity> element:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

Note

When viewing this xml within Android Studio, you may notice that the label element shows the actual text as defined in the strings.xml resource file. This is just a small example of enhancements in the new IDE.

How it works...

Declaring an activity is a simple matter of declaring the <activity> element and specifying the name of the activity class with the android:name attribute. By adding the <activity> element to the Android Manifest, we are specifying our intention to include this component within our application. Any activities (or any other component for that matter) that are not declared in the manifest will not be included in the application. Attempting to access or utilize an undeclared component will result in an exception being thrown at runtime.

In the preceding code, there is another attribute—android:label. This attribute indicates the title shown on the screen as well as the icon label if this is the Launcher activity.

Note

For a complete list of available application attributes, take a look at this resource:

http://developer.android.com/guide/topics/manifest/activity-element.html

Starting a new activity with an intent object

The Android application model can be seen as a service-oriented one, with activities as components and intents as the messages sent between them. Here, an intent is used to start an activity that displays the user's call log, but intents can be used to do many things and we will encounter them throughout this book.

Getting ready

To keep things simple, we are going to use an intent object to start one of Android's built-in applications rather than create a new one. This only requires a very basic application, so start a new Android project with Android Studio and call it ActivityStarter.

How to do it...

Again, to keep the example simple so that we can focus on the task at hand, we will create a function to show an intent in action and call this function from a button on our activity.

Once your new project is created in Android Studio, follow these steps:

  1. Open the MainActivity.java class and add the following function:
    public void launchIntent(View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://www.packtpub.com/"));
        startActivity(intent);
    }

    While you are typing this code, Android Studio will give this warning on View and intent: Cannot resolve symbol 'Intent'.

    This means that you need to add the library reference to the project. You can do this manually by entering the following code in the import section:

    import android.view.View;
    
    import android.content.Intent;

    Alternatively, just click on the words (in the red font), hit Alt + Enter, and let Android Studio add the library reference for you.

  2. Open the activity_main.xml file and add the following XML:
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Launch Browser"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="launchIntent"/>
    How to do it...
  3. Now it's time to run the application and see the intent in action. You will need to either create an Android emulator (in Android Studio, go to Tools | Android | AVD Manager) or connect a physical device to your computer.
  4. When you press the Launch Browser button, you will see the default web browser open with the URL specified.

How it works...

Though simple, this app demonstrates much of the power behind the Android OS. The intent object is just a message object. Intents can be used to communicate across your application's components (such as services and broadcast receivers) as well as with other applications on the device (as we did in this recipe).

Note

To test on a physical device, you may need to install drivers for your device (the drivers are specific to the hardware manufacturer). You will also need to enable Developer Mode on your device. Enabling Developer Mode varies according to the Android OS version. If you do not see the Developer Mode option in your device settings, open the About Phone option and begin tapping Build Number. After three taps, you should see a Toast message telling you that you are on your way to be a developer. Four more taps will enable the option.

In this recipe, we created an intent object by specifying ACTION_VIEW as what we want to do (our intention). You may have noticed that when you typed Intent and then the period, Android Studio provided a pop-up list of possibilities (this is the autocomplete feature), like this:

How it works...

ACTION_VIEW, along with a URL in the data, indicates that the intention is to view the website, so the default browser is launched (different data could launch different apps). In this example, our intent is just to view the URL, so we call the intent with just the startActivity() method. There are other ways to call the intent depending on our needs. In the Returning a result from an activity recipe, we will use the startActivityForResult() method.

There's more...

It's very common for Android users to download their favorite apps for web browsing, taking photos, text messaging, and so on. Using intents, you can let your app utilize your user's favorite apps instead of trying to reinvent all of this functionality.

See also

To start an activity from a menu selection, refer to the Handling menu selections recipe in Chapter 4, Menus.

Switching between activities

Often we will want to activate one activity from within another activity. Although this is not a difficult task, it will require a little more setting up to be done than the previous recipes as it requires two activities. We will create two activity classes and declare them both in the manifest. We'll also create a button, as we did in the previous recipe, to switch to the activity.

Getting ready

We'll create a new project in Android Studio, just as we did in the previous recipes, and call this one ActivitySwitcher. Android Studio will create the first activity, ActivityMain, and automatically declare it in the manifest.

How to do it...

  1. Since the Android Studio New Project wizard has already created the first activity, we just need to create the second activity. Open the ActivitySwitcher project and navigate to File | New | Activity | Blank Activity, as shown in this screenshot:
    How to do it...
  2. In the Customize the Activity dialog, you can leave the default Activity Name as it is, which is Main2Activity, or change it to SecondActivity, as shown here:
    How to do it...
  3. Open the MainActivity.java file and add the following function:
    public void onClickSwitchActivity(View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
  4. Now, open the activity_main.xml file located in the \res\layout folder and add the following XML to create the button:
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="Launch SecondActivity"
        android:onClick="onClickSwitchActivity"/>
  5. You can actually run the code at this point and see the second activity come up. We're going to go further and add a button to SecondActivity to close it, which will bring us back to the first activity. Open the SecondActivity.java file and add this function:
    public void onClickClose(View view) {
        finish();
    }
  6. Finally, add the Close button to the SecondActivity layout. Open the activity_second.xml file and add the following <Button> element just after the <TextView> element that was generated automatically:
    <Button
        android:id="@+id/buttonClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="onClickClose"/>
  7. Run the application on your device or emulator and see the buttons in action.

How it works...

The real work of this exercise is in the onClickSwitchActivity() method from Step 3. This is where we declare the second activity for the intent using SecondActivity.class. We went one step further by adding the close button to the second activity to show a common real-world situation—launching a new activity, then closing it, and returning to the original calling activity. This behavior is accomplished in the onClickClose() function. All it does is call finish(), but that tells the system that we're done with the activity. Finish doesn't actually return us to the calling activity or any specific activity for that matter; it just closes the current activity and relies on the back stack. If we want a specific activity, we can again use the intent object (we just change the class name while creating the intent).

This activity switching does not make a very exciting application. Our activity does nothing but demonstrate how to switch from one activity to another, which of course will form a fundamental aspect of almost any application that we develop.

If we had manually created the activities, we would need to add them to the manifest. By using these steps, Android Studio has already taken care of the XML. To see what Android Studio did, open the AndroidManifest.xml file and look at the <application> element:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER/>
    </intent-filter>
</activity>
<activity
    android:name=".SecondActivity"
    android:label="@string/title_activity_second">
</activity>

One thing to note in the preceding autogenerated code is that the second activity does not have the <intent-filter> element. The main activity is generally the entry point when starting the application. That's why MAIN and LAUNCHER are defined—so that the system will know which activity to launch when the application starts.

See also

  • To learn more about embedding widgets such as the Button, visit Chapter 3, Views, Widgets, and Styles.

Passing data to another activity

The intent object is defined as a messaging object. As a message object, its purpose is to communicate with other components of the application. In this recipe, we'll show you how to pass information with the intent and how to get it out again.

Getting ready

This recipe will pick up from where the previous one ended. We will call this project SendData.

How to do it...

Since this recipe is building on the previous recipe, most of the work is already done. We'll add an EditText element to the main activity so that we have something to send to SecondActivity. We'll use the (autogenerated) TextView view to display the message. Here are the complete steps:

  1. Open activity_main.xml, remove the existing <TextView> element, and add the following <EditText> element:
    <EditText
        android:id="@+id/editTextData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    The <Button> element that we created in the previous recipe doesn't change.

  2. Now, open the MainActivity.java file and change the onClickSwitchActivity() method as follows:
    public void onClickSwitchActivity(View view) {
        EditText editText = (EditText)findViewById(R.id.editTextData);
        String text = editText.getText().toString();
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra(Intent.EXTRA_TEXT,text);
        startActivity(intent);
    }
  3. Next, open the activity_second.xml file and modify the<TextView> element to include the ID attribute:
    <TextView
        android:id="@+id/textViewText"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  4. The last change is to edit the second activity to look for this new data and display it on the screen. Open SecondActivity.java and edit onCreate() as follows:
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        TextView textView = (TextView)findViewById(R.id.textViewText);
        if (getIntent()!=null && getIntent().hasExtra(Intent.EXTRA_TEXT)) {
            textView.setText(getIntent().getStringExtra(Intent.EXTRA_TEXT));
        }
    }
  5. Now run the project. Type some text on the main activity and press Launch Second Activity to see it send the data.

How it works...

As expected, the intent object is doing all the work. We created an intent just as in the previous recipe and then added some extra data. Did you notice the putExtra() method call? In our example, we used the already defined Intent.EXTRA_TEXT as the identifier, but we didn't have to. We can use any key we want (you've seen this concept before if you're familiar with name/value pairs).

The key point about using name/value pairs is that you have to use the same name to get the data back out. That's why we used the same key identifier when we read the extra data with getStringExtra().

The second activity was launched with the intent that we created, so it's simply a matter of getting the intent and checking for the data sent along with it. We do this in onCreate():

textView.setText(getIntent().getStringExtra(Intent.EXTRA_TEXT));

There's more...

We aren't limited to just sending String data. The intent object is very flexible and already supports basic data types. Go back to Android Studio and click on the putExtra method. Then hit Ctrl and the Spacebar. Android Studio will bring up the autocomplete list so that you can see the different data types that you can store.

Returning a result from an activity

Being able to start one activity from another is all well and good, but we will often need to know how the called activity has fared in its task or even which activity has been called. The startActivityForResult() method provides the solution.

Getting ready

Returning a result from an activity is not very different from the way we just called the activity in the previous recipes. You can either use the project from the previous recipe, or start a new project and call it GettingResults. Either way, once you have a project with two activities and the code needed to call the second activity, you're ready to begin.

How to do it...

There are only a few changes needed to get the results:

  1. First of all, open MainActivity.java and add the following constant to the class:
    public static final String REQUEST_RESULT="REQUEST_RESULT";
  2. Next, change the way the intent is called by modifying the onClickSwitchActivity() method to expect a result:
    public void onClickSwitchActivity(View view) {
        EditText editText = (EditText)findViewById(R.id.editTextData);
        String text = editText.getText().toString();
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra(Intent.EXTRA_TEXT,text);
        startActivityForResult(intent,1);
    }
  3. Then, add this new method to receive the result:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK) {
            Toast.makeText(this, Integer.toString(data.getIntExtra(REQUEST_RESULT, 0)), Toast.LENGTH_LONG).show();
        }
    }
  4. Finally, modify onClickClose in SecondActivity.java to set the return value as follows:
    public void onClickClose(View view) {
        Intent returnIntent = new Intent();
        returnIntent.putExtra(MainActivity.REQUEST_RESULT,42);
        setResult(RESULT_OK, returnIntent);
        finish();
    }

How it works...

As you can see, getting the results back is relatively straightforward. We just call the intent with startActivityForResult, so it knows that we want a result. We set up the onActivityResult() callback handler to receive the results. Finally, we make sure that the second activity returns a result with setResult() before closing the activity. In this example, we are just setting a result with a static value. We just display what we receive to demonstrate the concept.

It's good practice to check the result code to make sure that the user didn't cancel the action. It's technically an integer, but the system uses it as a boolean value. Check for either RESULT_OK or RESULT_CANCEL and proceed accordingly. In our example, the second activity doesn't have a cancel button, so why bother to check? What if the user hits the back button? The system will set the result code to RESULT_CANCEL and the intent to null, which will cause our code to throw an exception.

We made use of the Toast object, which is a convenient pop-up message that can be used to unobtrusively notify the user. It also functions as a handy method for debugging as it doesn't need a special layout or screen space.

There's more...

Besides the result code, onActivityResults() also includes a Request Code. Are you wondering where that came from? It is simply the integer value that was passed with the startActivityForResult() call, which takes this form:

startActivityForResult(Intent intent, int requestCode);

We didn't check the request code because we knew we had only one result to handle—but in trivial applications with several activities, this value can be used to identify where the request originated.

Tip

If startActivityForResult() is called with a negative request code, it will behave exactly as if it were a call to startActivity()—that is, it will not return a result.

See also

  • To learn more about creating new activity classes, refer to the Switching between activities recipe
  • For more information about Toasts, check out the Making a Toast recipe in Chapter 7, Alerts and Notifications

Saving an activity's state

The mobile environment is very dynamic, with users changing tasks much more often than on desktops. With generally fewer resources on a mobile device, it should be expected that your application will be interrupted at some point. It's also very possible that the system will shut down your app completely to give additional resources to the task at hand. It's the nature of mobiles.

A user might start typing something in your app, be interrupted by a phone call, or switch over to another app to send a text message, and by the time they get back to your app, the system may have closed it down completely to free up the memory. To provide the best user experience, you need to expect such behavior and make it easier for your user to resume from where they left off. The good thing is that the Android OS makes this easier by providing callbacks to notify your app of state changes.

Note

Simply rotating your device will cause the OS to destroy and recreate your activity. This might seem a bit heavy-handed, but it's done for good reason—it's very common to have different layouts for portrait and landscape, so this ensures that your app is using the correct resources.

In this recipe, you'll see how to handle the onSaveInstanceState() and onRestoreInstanceState() callbacks to save your application's state. We will demonstrate this by creating a counter variable and increment it each time the Count button is pressed. We will also have an EditText and a TextView widget to see their default behavior.

Getting ready

Create a new project in Android Studio and name it StateSaver. We need only a single activity, so the autogenerated main activity is sufficient. However, we will need a few widgets, including EditText, Button, and TextView. Their layout (in activity_main.xml) will look like this:

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"/>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Count"
    android:onClick="onClickCounter"/>

<TextView
    android:id="@+id/textViewCounter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/button"/>

How to do it...

Perform the following set of steps:

  1. To keep track of the counter, we need to add a global variable to the project, along with a key for saving and restoring. Add the following code to the MainActivity.java class:
    static final String KEY_COUNTER = "COUNTER";
    private int mCounter=0;
  2. Then add the code needed to handle the button press; it increments the counter and displays the result in the TextView widget:
    public void onClickCounter(View view) {
        mCounter++;
        ((TextView)findViewById(R.id.textViewCounter)).setText("Counter: " + Integer.toString(mCounter));
    }
  3. To receive notifications of application state change, we need to add the onSaveInstanceState() and onRestoreInstanceState() methods to our application. Open MainActivity.java and add the following:
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_COUNTER,mCounter);
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mCounter=savedInstanceState.getInt(KEY_COUNTER);
    }
  4. Run the program and try changing the orientation to see how it behaves (if you're using the emulator, Ctrl + F11 will rotate the device).

How it works...

All activities go through multiple states during their lifetime. By setting up callbacks to handle the events, we can have our code save important information before the activity is destroyed.

Step 3 is where the actual saving and restoring occurs. The system sends a Bundle (a data object that also uses name/value pairs) to the methods. We use the onSaveInstanceState() callback to save the data and pull it out in the onRestoreInstanceState() callback.

But wait! Did you try typing text in the EditText view before rotating the device? If so, you'd have noticed that the text was also restored, but we don't have any code to handle that view. By default, the system will automatically save the state, provided it has a unique ID (not all views automatically have their state saved, such as the TextView, but we can manually save it if we want).

Tip

Note that if you want Android to automatically save and restore the state of a view, it must have a unique ID (specified with the android:id= attribute in the layout). Beware; not all view types automatically save and restore the state of a view.

There's more...

The onRestoreInstanceState() callback is not the only place where the state can be restored. Look at the signature of onCreate():

onCreate(Bundle savedInstanceState)

Both methods receive the same Bundle instance named savedInstanceState. You could move the restore code to the onCreate() method and it would work the same. But one catch is that the savedInstanceState bundle will be null if there is no data, such as during the initial creation of the activity. If you want to move the code from the onRestoreInstanceState() callback, just check to make sure that the data is not null, as follows:

if (savedInstanceState!=null) {
    mCounter = savedInstanceState.getInt(KEY_COUNTER);
}

See also

  • The Storing persistent activity data recipe will introduce persistent storage.
  • Take a look at Chapter 6, Working with Data, for more examples on Android activities.
  • The Understanding the activity lifecycle recipe explains the Android Activity Lifecycle.

Storing persistent activity data

Being able to store information about our activities on a temporary basis is very useful, but more often than not, we will want our application to remember information across multiple sessions.

Android supports SQLite, but that could be a lot of overhead for simple data, such as the user's name or a high score. Fortunately, Android also provides a lightweight option for these scenarios, with SharedPreferences.

Getting ready

You can either use the project from the previous recipe or start a new project and call it PersistentData (in a real-world application, you'll likely be doing both anyway). In the previous recipe, we saved mCounter in the session state. In this recipe, we'll add a new method to handle onPause() and save mCounter to SharedPreferences. We'll restore the value in onCreate().

How to do it...

We have only two changes to make, and both are in MainActivity.java:

  1. Add the following onPause() method to save the data before the activity closes:
    @Override
    protected void onPause() {
        super.onPause();
    
        SharedPreferences settings = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(KEY_COUNTER, mCounter);
        editor.commit();
    }
  2. Then add the following code at the end of onCreate() to restore the counter:
    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    
    int defaultCounter = 0;
    mCounter = settings.getInt(KEY_COUNTER, defaultCounter);
  3. Run the program and try it out.

How it works...

As you can see, this is very similar to saving state data, because it also uses name/value pairs. Here, we just stored an int, but we can just as easily store one of the other primitive data types. Each data type has equivalent getters and setters, for example, SharedPreferences.getBoolean() or SharedPreferences.setString().

Saving our data requires the services of SharedPreferences.Editor. This is evoked with edit() and accepts remove() and clear() procedures as well as setters such as putInt(). Note that we must conclude any storing that we do here with the commit() statement.

There's more...

There is a slightly more sophisticated variant of the getPreferences() accessor: getSharedPreferences(). It can be used to store multiple preference sets.

Using more than one preference file

Using getSharedPreferences() is no different from using its counterpart, but it allows for more than one preference file. It takes the following form:

getSharedPreferences(String name, int mode)

Here, name is the file. The mode can be either MODE_PRIVATE, MODE_WORLD_READABLE, or MODE_WORLD_WRITABLE and describes the file's access levels.

See also

  • Chapter 6, Working with Data, for more examples on data storage

Understanding the activity lifecycle

The Android OS is a dangerous place for an activity. The demand for resources on a battery-operated platform is managed quite ruthlessly by the system. Our activities can be dumped from memory when it's running low, without even a moment's notice and along with any data they contain. Therefore, it is essential to understand the activity lifecycle.

The following diagram shows the stages through which an activity passes during its lifetime:

Understanding the activity lifecycle

Along with the stages, the diagram also shows the methods that can be overridden. As you can see, we've already utilized most of these methods in the preceding recipes. Hopefully, getting the big picture will help in your understanding.

Getting ready

Create a new project in Android Studio with a Blank Activity, and call it ActivityLifecycle. We will use the (autogenerated) TextView method to display the state information.

How to do it...

To see the application move through the various stages, we will create methods for all the stages:

  1. Open activity_main.xml and add an ID to the autogenerated TextView:
    android:id="@+id/textViewState"
  2. The remaining steps will be in MainActivity.java. Add the following global declaration:
    private TextView mTextViewState;
  3. Modify the onCreate() method to save TextView and set the initial text:
    mTextViewState = (TextView)findViewById(R.id.textViewState);
    mTextViewState.setText("onCreate()\n");
  4. Add the following methods to handle the remaining events:
    @Override
    protected void onStart() {
        super.onStart();
        mTextViewState.append("onStart()\n");
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        mTextViewState.append("onResume()\n");
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        mTextViewState.append("onPause()\n");
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        mTextViewState.append("onStop()\n");
    }
    
    @Override
    protected void onRestart() {
        super.onRestart();
        mTextViewState.append("onRestart()\n");
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTextViewState.append("onDestroy()\n");
    }
  5. Run the application and observe what happens when the activity is interrupted by pressing the Back and Home keys. Try other actions, such as task switching, to see how they impact your application.

How it works...

Our activity can exist in one of these three states: active, paused, or stopped. There is also a fourth state, destroyed, but we can safely ignore it:

  • An activity is in the active state when its interface is available for the user. It persists from onResume() until onPause(), which is brought about when another activity comes to the foreground. If this new activity does not entirely obscure our activity, then ours will remain in the paused state until the new activity is finished or dismissed. It will then immediately call onResume() and continue.
  • When a newly started activity fills the screen or makes our activity invisible, then our activity will enter the stopped state, and the resumption will always invoke a call to onRestart().
  • When an activity is in either the paused or stopped state, the operating system can (and will) remove it from the memory when the memory is low or when other applications demand it.
  • It is worth noting that we never actually see the results of the onDestroy() method, as the activity is removed by this point. If you want to explore these methods further, then it is well worth employing Activity.isFinishing() to see whether the activity is really finishing before onDestroy() is executed, as seen in the following snippet:
    @Override
      public void onPause() {
      super.onPause();
      mTextView.append("onPause()\n ");
      if (isFinishing()){
        mTextView.append(" ... finishing");
      }
    }

Tip

When implementing these methods, always call the superclass before doing any work.

There's more...

Shutting down an activity

To shut down an activity, directly call its finish() method, which in turn calls onDestroy(). To perform the same action from a child activity, use finishFromChild(Activity child), where child is the calling subactivity.

It is often useful to know whether an activity is being shut down or merely paused, and the isFinishing(boolean) method returns a value that indicates which of these two states the activity is in.

Left arrow icon Right arrow icon

Key benefits

  • Find the answers to your common Android programming problems, from set up to security, to help you deliver better applications, faster
  • Uncover the latest features of Android Marshmallow to make your applications stand out
  • Get up to speed with Android Studio 1.4 - the first Android Studio based on the IntelliJ IDE from JetBrains

Description

The Android OS has the largest installation base of any operating system in the world; there has never been a better time to learn Android development to write your own applications, or to make your own contributions to the open source community! This “cookbook” will make it easy for you to jump to a topic of interest and get what you need to implement the feature in your own application. If you are new to Android and learn best by “doing,” then this book will provide many topics of interest. Starting with the basics of Android development, we move on to more advanced concepts, and we’ll guide you through common tasks developers struggle to solve. The first few chapters cover the basics including Activities, Layouts, Widgets, and the Menu. From there, we cover fragments and data storage (including SQLite), device sensors, the camera, and GPS. Then we move on more advanced topics such as graphics and animation (including OpenGL), multi-threading with AsyncTask, and Internet functionality with Volley. We’ll also demonstrate Google Maps and Google Cloud Messaging (also known as Push Notifications) using the Google API Library. Finally, we’ll take a look at several online services designed especially for Android development. Take your application big-time with full Internet web services without having to become a server admin by leveraging the power of Backend as a Service (BaaS) providers.

Who is this book for?

If you are new to Android development and want to take a hands-on approach to learning the framework, or if you are an experienced developer in need of clear working code to solve the many challenges in Android development, you can benefit from this book. Either way, this is a resource you’ll want to keep at your desk for a quick reference to solve new problems as you tackle more challenging projects.

What you will learn

  • Along with Marshmallow, get hands-on working with Google's new Android Studio IDE
  • Develop applications using the latest Android framework while maintaining backward-compatibility with the support library
  • Master Android programming best practices from the recipes
  • Create exciting and engaging applications using knowledge gained from recipes on graphics, animations, and multimedia
  • Work through succinct steps on specifics that will help you complete your project faster
  • Keep your app responsive (and prevent ANRs) with examples on the AsynchTask class
  • Utilize Google Speech Recognition APIs for your app.
  • Make use of Google Cloud Messaging (GCM) to create Push Notifications for your users
  • Get a better understanding of the Android framework through detailed explanations

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 31, 2016
Length: 428 pages
Edition : 2nd
Language : English
ISBN-13 : 9781785886195
Vendor :
Google
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Mar 31, 2016
Length: 428 pages
Edition : 2nd
Language : English
ISBN-13 : 9781785886195
Vendor :
Google
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 142.97
Android Programming for Beginners
$54.99
Learning Material Design
$38.99
Android Application Development Cookbook
$48.99
Total $ 142.97 Stars icon
Banner background image

Table of Contents

16 Chapters
1. Activities Chevron down icon Chevron up icon
2. Layouts Chevron down icon Chevron up icon
3. Views, Widgets, and Styles Chevron down icon Chevron up icon
4. Menus Chevron down icon Chevron up icon
5. Exploring Fragments, AppWidgets, and the System UI Chevron down icon Chevron up icon
6. Working with Data Chevron down icon Chevron up icon
7. Alerts and Notifications Chevron down icon Chevron up icon
8. Using the Touchscreen and Sensors Chevron down icon Chevron up icon
9. Graphics and Animation Chevron down icon Chevron up icon
10. A First Look at OpenGL ES Chevron down icon Chevron up icon
11. Multimedia Chevron down icon Chevron up icon
12. Telephony, Networks, and the Web Chevron down icon Chevron up icon
13. Getting Location and Using Geofencing Chevron down icon Chevron up icon
14. Getting your app ready for the Play Store Chevron down icon Chevron up icon
15. The Backend as a Service Options Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5
(6 Ratings)
5 star 66.7%
4 star 16.7%
3 star 16.7%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Amazon Customer Feb 10, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Overall, I really like this book.The recipes are very concise and easy to follow.Only one drawback is, some recipes are not available for API 23+ because of Google's new policies.In that kind of case, I would like to recommend using API 22 or lower emulator or device.
Amazon Verified review Amazon
Hugo May 15, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is a must have if you are an Android developer or want to be, the author's have covered almost everything from the basic/intermediate/advanced level topics in an Android app and split all this content in recipes as the title of the book already suggest this is a true cookbook, I think that real examples better learning for me, than just read the documentation, this book have a great structure and the examples are totally expandable, so you can adapt to your own projects and be able to produce any kind of application you want to and speed your learning process. The recipes are very rich on each topic of application development covering examples of activities, layouts, network, multimedia, data, back end services, graphics, Play Store API's, geofencing and much more. Both of the authors have a great background on Android development and I have to say that the book is very good to help every Android developer.
Amazon Verified review Amazon
Amazon Customer Jan 11, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
GREAT Android development book! A rare combination of an easy to understand programming book that is also comprehensive in its coverage of Android app development. What I really like about the Android Application Development Cookbook is that it not only shows you the code for each cookbook recipe but then it goes through the code examples and explains exactly what each line does.Great way to learn Android/Java while creating usable programs. Outstanding!
Amazon Verified review Amazon
Emil Atanasov May 12, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
[Disclosure]I'm one of the technical reviewers of this book. The book covers all the basics of Android development and a few advanced topics which every developer faces when starting developing mobile applications. Each chapter discusses different parts of the framework in detail. It presents short recipes, how to solve common problems by introducing different parts of the Android framework. By combining most recipes in the end you will be able to write pretty complex applications. Those apps will incorporate different views, displaying data, geo-locations, checking for permissions, fetching data from servers and you will be able to integrate 3rd party services.The book is a comprehensive list of recipes, each one - explains step by step a solution of a common problem. The explanation is supported with code example(s) and screen shots, which guide you through the process of implementation.Different chapters discuss most important topics like - layouts and views, fragments and menus, working with data - local and external, also presenting media and how to upload a final .apk to the play store and how to use third party services.My favorite feature parts of the book are the pieces where the implementation is compliant with the new Android Marshmallow.
Amazon Verified review Amazon
Creosote Apr 29, 2017
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
A nice, lead-by-example book. More software books should take this approach.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.