Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android Application Development Cookbook

You're reading from   Android Application Development Cookbook Over 100 recipes to help you solve the most common problems faced by Android Developers today

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher
ISBN-13 9781785886195
Length 428 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Rick Boyer Rick Boyer
Author Profile Icon Rick Boyer
Rick Boyer
Kyle Mew Kyle Mew
Author Profile Icon Kyle Mew
Kyle Mew
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

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

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.
You have been reading a chapter from
Android Application Development Cookbook - Second Edition
Published in: Mar 2016
Publisher:
ISBN-13: 9781785886195
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime