Setting a Parent Activity, 46/100 Days of Code
Setting a parent Activity can make your app much more user-friendly.
Simplifying your app’s user interface is one of the great advantages of setting a parent Activity. Today, I show you step by step how to add one to your apps. You can find a video of this at the bottom of the page, a code snippet below, and a link to my source code here.
NOTE: Remember to click Apply Changes in Android Studio to update your emulator!
Beginner: You can download this project from the link above and open it in Android Studio while you follow along with the video.
Advanced: I’ve included steps throughout the page along with code snippets. Feel free to use the video as a reference as well.
Requirements
- Android Studio and a newly created project with an empty Activity
- An Android Virtual Device
- BONUS: An IDE of your preference if you don’t want to use Android Studio to edit your code
Step 1: Update the Second and Third Activity tags with the parentActivityName XML attribute. See the snippet below for the entire AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.clickingactivities"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <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=".Second" android:parentActivityName=".MainActivity"></activity> <activity android:name=".Third" android:parentActivityName=".MainActivity"></activity> </application> </manifest>
It’s that easy! By defining the parent Activity for the Second and Third Activities, the Activities become easier to navigate. To see this in action, watch the YouTube video below.
Join the mailing list to see updates like this every week!
Coding Fanatic