Chapter 15. Android Intent and Persistence

In this chapter, we will look at a couple of different ways to save data to the Android device's permanent storage. Also, for the first time, we will add a second Activity to our app. It often makes sense when implementing a separate "screen," like a settings screen in our app, to do so in a new Activity. We could go to the trouble of hiding the original UI and then showing the new UI, but this would quickly lead to confusing and error-prone code. So, we will see how to add an Activity and navigate the user between them.

In this chapter, we will:

  • Learn about Android Intents to switch the Activity and pass data
  • Create a settings screen in a new Activity
  • Persist the settings screen data using the SharedPreferences class
  • Learn about JavaScript object notation for serialization
  • Explore Java try-catch-finally
  • Implement saving data in our Note To Self app

Good Intents

The Intent class is appropriately named. It is a class that demonstrates the Intent of an Activity from our app. It makes clear Intent and it also facilitates it.

All our apps so far have had just one Activity, but most Android apps are comprised of more than one.

In perhaps its most common use, an Intent object allows us to switch between activities. But, of course, activities are classes. So, what happens to their data when we switch between them? Intents handle this problem for us, as well, by allowing us to pass data between activities.

Intents aren't just about wiring up the activities of our app. They also make it possible to interact with other apps. For example, we could provide a link in our app for the user to send e-mail, make a phone call, interact with social media, and open a web page in a browser and have the e-mail, dialer, browser, or relevant social media app do all the work.

There aren't enough pages to really dig deep into interacting with other apps, and we will mainly focus on switching between activities and passing data.

Tip

If you want to know more about the Intent class than this chapter has the time to cover, I recommend the book Learning Android Intents by Muhammad Usama bin Aftab and Wajahat Karim, published by Packt Publishing (https://www.packtpub.com/application-development/learning-android-intents). The book assumes you already know how to program in Java but has a very gentle learning curve and explores the Intent class in vastly greater detail than we have room for in this book.

Throughout the rest of the book, we will occasionally bump into the Intent class again and see more of the things it can do.

Switching Activity

Let's say we have an app with two activities, because we will soon. We can assume that, as usual, we have an Activity called MainActivity, which is where the app starts, and a second Activity called SettingsActivity. This is how we can swap from MainActivity to SettingsActivity:

// Declare and initialize a new Intent object called myIntent
Intent myIntent = new   Intent(this,SettingsActivity.class);

// Switch to the SettingsActivity
startActivity(myIntent);

Look carefully at how we initialized the Intent object. The Intent class has a constructor that takes two arguments. The first is a reference to the current Activity, this. And the second parameter is the name of the Activity we want to open, SettingsActivity.class. The .class on the end of SettingsActivity makes it the full name of the Activity, as declared in the AndroidManifest.xml file, and we will peek at that when we experiment with Intents shortly.

The only problem is that SettingsActivity doesn't share any of the data of MainActivity. In a way, this is a good thing because if you need all the data from MainActivity, then it is a reasonable indication that switching activities might not be the best way of proceeding with your app's design. It is, however, unreasonable to have encapsulation so thorough that the two activities know absolutely nothing about each other.

Passing data between activities

What if we have a sign-in screen for the user and we want to pass the login credentials to each and every Activity of our app? We could do so using Intents.

We can add data to Intent like this:

// Create a String called username 
// and set its value to bob
String username = "Bob";

// Create a new Intent as we have already seen
Intent myIntent = new Intent(this,SettingsActivity.class);

// Add the username String to the Intent
// using the putExtra method of the Intent class
myIntent.putExtra("USER_NAME", username);

// Start the new Activity as we have before
startActivity(myIntent);

In SettingsActivity, we could then retrieve the string like this:

// Here we need an Intent also
// But the default constructor will do
// as we are not switching Activity
Intent myIntent = new Intent();

// Initialize username with the passed in String 
String username = intent.getExtra().getStringKey("USER_NAME");

In the previous two blocks of code, we switched the Activity in the same way as we have already seen. But before we called startActivity, we used the putExtra method to load a string into the Intent.

We add data using key-value pairs. Each piece of data needs to be accompanied by an identifier that can be used in the retrieving Activity to identify and retrieve the data.

The identifier name is arbitrary, but useful/memorable values should be used.

Then, in the receiving Activity, we simply create Intent using the default constructor:

Intent myIntent = new Intent();

And we can then retrieve the data using the getExtras method and the appropriate identifier from the key-value pair.

Once we want to start sending more than a few values, then it is worth considering different tactics.

The Intent class can help us in sending more complex data than this, but the Intent class does have its limits. For example, we wouldn't be able to pass a Note object.

We can also save data (complex and simple) to the device's disk, and this is covered later in this chapter. We can even create and use a database dedicated to our app. Databases will be primarily discussed in Chapter 23, Using SQLite Databases in Our Apps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.119.199