In this chapter, we will look at a couple of different ways to save data to an 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," such as 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 summary, in this chapter, we will do the following:
Activity
and pass dataActivity
SharedPreferences
classtry
-catch
-finally
The Intent
class is appropriately named. It is a class that demonstrates the intent of an Activity
from our app. It makes intent clear and it also facilitates it.
All our apps so far have had just one Activity
, but many Android apps are comprised of more than one.
In perhaps its most common use, an Intent
allows us to switch between Activities. But, of course, Activities are classes. So, what happens to the 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, too. For example, we could provide a link in our app for the user to send an email, make a phone call, interact with social media, or open a web page in a browser, and have the email, dialler, web 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 so we will mainly focus on switching between Activities and passing data.
Let's say we have an app with two Activity
-based classes, 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. Intent
has a constructor that takes two arguments. The first is a reference to the current Activity
, this
. 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.
What if we have a sign-in screen for the user and we want to pass the login credentials to each Activity
of our app? We could do so using Intents.
We can add data to an 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 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 an Intent
using the default constructor:
Intent myIntent = new Intent();
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, it is worth considering different tactics.
The Intent
class can help us send more complex data than this, but the Intent
class has its limits. For example, we wouldn't be able to send a Note
object.
3.144.250.169