Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Android 9 Development Cookbook
Android 9 Development Cookbook

Android 9 Development Cookbook: Over 100 recipes and solutions to solve the most common problems faced by Android developers , Third Edition

eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Android 9 Development Cookbook

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 life cycle

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. Although 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).

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 node is how we tell the OS about our Activity class 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, now at version 3.2, is used for all the code samples shown in this book. If you have not already installed it, visit the Android Studio website (see the link in the previous tip) to install the IDE and the SDK bundle for your platform.

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:

  1. Click on the Start a new Android Studio project option.
  2. Enter an application name; for this example, we used DeclareAnActivity. Click on Next:

  1. In the Target Android Devices dialog, you can leave the Phone and Tablet checkbox selected with the default API 21: Android 5.0 (Lollipop) selection for the minimum SDK (for this example, it really doesn't matter which API level you choose, as activities have existed since API level 1). Click on Next:

  1. In the Add an Activity to Mobile dialog, select the Empty Activity option. Click on Next:

  1. In the Configure Activity dialog, you can leave the defaults as provided, but note that 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 Empty Activity option (in step 5). 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>
When viewing this xml in Android Studio, you may notice that the label element shows the actual text (DeclareAnActivity in this case) as defined in the strings.xml resource file.

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 in our application. Any activities (or any other component for that matter) that are not declared in the manifest will not be available to 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 if this is the Launcher activity.

For a complete list of available Activity 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.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
Alternatively, let Android Studio add the library reference for you: just click on the code highlighted with a red font and press Alt + Enter.
  1. Open the activity_main.xml file and replace the <TextView /> block with the following XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch Browser"
android:id="@+id/button"
android:onClick="launchIntent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
  1. 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 AVDManager) or connect a physical device to your computer.
  2. 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. An intent is 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. In this recipe, we asked the OS to start any app that could handle the data we specified with the setData() method. (If the user has multiple browsers installed and no default set, the OS will show a list of apps for the user to choose from.)

To test this 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 being a developer. Four more taps will enable the option.

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

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, we just want to open a browser with the specified URL, so we call 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 allow your users to use their 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 and Action Mode.

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 | Empty Activity, as shown in this screenshot:
  1. In the New Android Activity dialog, you can leave the default Activity Name as is, or change it to SecondActivity, as follows:
  1. Open the MainActivity.java file and add the following function:
    public void onClickSwitchActivity(View view) { 
        Intent intent = new Intent(this, SecondActivity.class); 
        startActivity(intent); 
    }
  1. Now, open the activity_main.xml file located in the res/layout folder and replace the <TextView /> with 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 Second Activity"
android:onClick="onClickSwitchActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
  1. You can run the code at this point and see the second activity open. 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(); 
    } 
  1. Finally, add the Close button to the SecondActivity layout. Open the activity_second.xml file and add the following <Button> element to the auto-generated ConstraintLayout
    <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"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
  1. 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 returning to the original calling activity. This behavior is accomplished in the onClickClose() function. All it does is call finish(), but that tells the OS 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 application's back stack to show the last activity. If we want a specific activity, we can again use the Intent object and specify the activity class name when creating the Intent.

This activity switching does not make a very exciting application. Our activity does nothing but demonstrates 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. Using the New Android Activity wizard will automatically add the necessary elements to the Android Manifest file. To see what Android Studio did for you, open the AndroidManifest.xml file and look at the <application> element:

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

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

One thing to note in the preceding auto-generated 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 2, 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 (auto-generated) TextView view to display the message. The following are the complete steps:

  1. Open activity_main.xml and add the following <EditText> element above the button:
<EditText
android:id="@+id/editTextData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/button" />

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

  1. 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); 
}
  1. Next, open the activity_second.xml file and add the following <TextView> element:
<TextView
android:id="@+id/textViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/buttonClose"/>
  1. 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));
}
}
  1. Now, run the project. Type some text in 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 auto-complete 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 very useful and commonly used, but there are times when we need to know the result from the called activity. 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"; 
  1. 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);
}
  1. 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();
}
}
  1. 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, indicating we want a result back. 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 use a simple Toast to display the result back to the user.

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? Android will set the result code to RESULT_CANCEL and the intent to null, which would cause our code to throw an exception if we attempt to access the null result.

We made use of the Toast object, which displays a convenient pop-up message 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 non-trivial applications with several activities, this value can be used to identify which Activity is returning a result.

If startActivityForResult() is called with a negative request code, it will behave the same as if we used 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 8, 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 send a text message, and by the time they get back to your app, the OS may have closed your app 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.

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 a 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 auto-generated main activity is sufficient. However, we will need a few widgets, including EditText, Button, and TextView. Their layout (in activity_main.xml) will be as follows:

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/button"/>

<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"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

<TextView
android:id="@+id/textViewCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/button"
app:layout_constraintBottom_toBottomOf="parent"/>

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; 
  1. 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));
}
  1. 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); 
} 
  1. 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 OS 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.

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). Bur 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. Here's how that code would look:

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 7, Data Storage, for more examples on how to persist data
  • The Understanding the activity life cycle recipe explains the Android Activity life cycle

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(In a real-world application, you'll likely use both options for saving data.)

Getting ready

You can either use the project from the previous recipe or start a new project and call it PersistentData. 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();
}
  1. 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);
((TextView)findViewById(R.id.textViewCounter))
.setText("Counter: " + Integer.toString(mCounter));
  1. 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 changes 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 7, Data Storage, for more examples on data storage

Understanding the activity life cycle

As mobile hardware continues to improve, so too does the demand placed on that hardware. With increasingly more powerful applications and user multi-tasking, the already limited resources can be quite challenging. The Android OS has many features built in to help the user get the best performance from their device, such as limiting background processes, disabling application notifications, and allowing data limits. The OS will also manage application lifetime based on foreground tasks. If your application is in the foreground, the life cycle is straightforward. But as soon as your user switches tasks and your application is moved to the background, understanding the Android application life cycle becomes very important.

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

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 your understanding.

Getting ready

Create a new project in Android Studio with a Blank Activity, and call it ActivityLifecycle. We will use the (auto-generated) 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 auto-generated TextView:
android:id="@+id/textViewState" 
  1. The remaining steps will be in MainActivity.java. Modify the onCreate() method to set the initial text:
((TextView)findViewById(R.id.textViewState)).setText("onCreate()n");
  1. Add the following methods to handle the remaining events:
@Override
protected void onStart() {
super.onStart();
((TextView)findViewById(R.id.textViewState)).append("onStart()\n");
}

@Override
protected void onResume() {
super.onResume();
((TextView)findViewById(R.id.textViewState)).append("onResume()\n");
}

@Override
protected void onPause() {
super.onPause();
((TextView)findViewById(R.id.textViewState)).append("onPause()\n");
}

@Override
protected void onStop() {
super.onStop();
((TextView)findViewById(R.id.textViewState)).append("onStop()\n");
}

@Override
protected void onRestart() {
super.onRestart();
((TextView)findViewById(R.id.textViewState)).append("onRestart()\n");
}

@Override
protected void onDestroy() {
super.onDestroy();
((TextView)findViewById(R.id.textViewState)).append("onDestroy()\n");
}
  1. 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 there's no guarantee the OS will ever call 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 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();
((TextView)findViewById(R.id.textViewState)).append("onPause()\n");
if (isFinishing()){
((TextView)findViewById(R.id.textViewState)).append(" ... finishing");
}
}
When implementing these methods, always call the superclass before doing any work.

There's more...

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
Download code icon Download Code

Key benefits

  • Uncover the latest features in Android 9 Pie to make your applications stand out
  • Develop Android Pie applications with the latest mobile technologies, from set up to security
  • Get up-to-speed with Android Studio 3 and its impressive new features

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! With this extensively updated cookbook, you'll find solutions for working with the user interfaces, multitouch gestures, location awareness, web services, and device features such as the phone, camera, and accelerometer. You also get useful steps on packaging your app for the Android Market. Each recipe provides a clear solution and sample code you can use in your project from the outset. Whether you are writing your first app or your hundredth, this is a book that you will come back to time and time again, with its many tips and tricks on the rich features of Android Pie.

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 will benefit from this book. Either way, this is a resource you'll want to keep on your desk as a quick reference to help you solve new problems as you tackle more challenging projects.

What you will learn

  • Develop applications using the latest Android framework while maintaining backward-compatibility with the support library
  • Create 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
  • Add location awareness to your own app with examples using the latest Google Play services API
  • Utilize Google Speech Recognition APIs for your app
Estimated delivery fee Deliver to Malta

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 19, 2018
Length: 464 pages
Edition : 3rd
Language : English
ISBN-13 : 9781788991216
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Estimated delivery fee Deliver to Malta

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

Product Details

Publication date : Oct 19, 2018
Length: 464 pages
Edition : 3rd
Language : English
ISBN-13 : 9781788991216
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 99.97
Android Programming for Beginners
€36.99
Android 9 Development Cookbook
€32.99
Android Programming with Kotlin for Beginners
€29.99
Total 99.97 Stars icon

Table of Contents

17 Chapters
Activities Chevron down icon Chevron up icon
Layouts Chevron down icon Chevron up icon
Views, Widgets, and Styles Chevron down icon Chevron up icon
Menus and Action Mode Chevron down icon Chevron up icon
Fragments Chevron down icon Chevron up icon
Home Screen Widgets, Search, and the System UI Chevron down icon Chevron up icon
Data Storage Chevron down icon Chevron up icon
Alerts and Notifications Chevron down icon Chevron up icon
Using the Touchscreen and Sensors Chevron down icon Chevron up icon
Graphics and Animation Chevron down icon Chevron up icon
A First Look at OpenGL ES Chevron down icon Chevron up icon
Multimedia Chevron down icon Chevron up icon
Telephony, Networks, and the Web Chevron down icon Chevron up icon
Location and Using Geofencing Chevron down icon Chevron up icon
Getting Your App Ready for the Play Store Chevron down icon Chevron up icon
Getting Started with Kotlin Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Most Recent
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.4
(9 Ratings)
5 star 55.6%
4 star 0%
3 star 11.1%
2 star 0%
1 star 33.3%
Filter icon Filter
Most Recent

Filter reviews by




Amazon Customer Aug 04, 2022
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
The structure of the book is well thought out but when one works through the recipes its full of errors in the code; whole sections are misplaced or missing. Packt's errata web page is inaccessible for some reason and Packt will not respond to customer support questions. Don't waste your money.
Amazon Verified review Amazon
Hieu Quang Pham Jan 14, 2022
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
This book is hurting my eyes. The screen shots are not viewable as the Android Studio theme on the Author's computer is dark with small fonts. I wish the author had used light theme with bigger font to be more reader friendly. The code for some recipes seemed to have extra methods that would not be built successfully in Android Studio Arctic Fox 2020.3.1. I could get by with this book because I had previous experience with Android App Development. My friend had a hard time from chapter 3 on and gave up as he could not read the text on the screen shots.
Amazon Verified review Amazon
Paul Plachetka Aug 06, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Das Buch ist äußerst einfach verständlich für Leute die nicht gerade neu in Java sind und komplett von vorn mit Android Development beginnen wollen. Auch wenn es um Android 9 geht, ist es ziemlich aktuell, in Android 10 ist jetzt nicht sehr viel dazu gekommen. Die Kapitel sind von Einführung, über erste Elemente und Konzepte zu komplexeren Techniken geordnet und sind gut verständlich. Ich bin zwar noch nicht durch mit dem Buch, dennoch lässt sich auch bedingt durch die simple IDE, die Einem schon viel abnimmt, sehr viel selbst anfangen. Das einzige Manko das ich hätte, wäre, dass die Bebilderung in Schwarz-Weiß ist und die IDE im Dark Mode abbildet, was das erkennen von vermeintlich gemeinten Details in der IDE sehr schwer macht.
Amazon Verified review Amazon
Josh May 07, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm very impressed with this book. The author gets straight to the point, and covers plenty of information without covering too much or too little. I highly recommend this book.
Amazon Verified review Amazon
Amazon Customer Feb 27, 2020
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
This book is for newly comers and the content is the same as other versions I have seen. You can do more using YouTube tutorials than purchasing this book.Not a good one
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 the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela