Activities and Fragments Flashcards
Why extend AppCompatActivity?
AppCompatActivity is a subclass of Activity that supports all modern Android features while providing backward compatibility with older versions of Android.
What does AppCompatActivity.setContentView( ) do?
Specifies AND inflates the layout xml associated with an Activity
What is the name of the bar featuring clock, battery, signal etc
Status bar
What is the difference between the following?
ActionBar
AppBar
Toolbar
action bar = app bar
Toolbar succeeded ActionBar in API 21 (Lollipop)
Toolbar is more customisable.
Using Toolbar in API < 21 requires
^androidx.appcompat.widget.Toolbar/^ wrapped in an ^AppBarLayout/^
What does the navigation component enable?
Which 2 dependencies are required to use the navigation component?
Navigation component enables:
- Complex navigation
- Transition animation
- Deep Linking
- Compile-time checked argument passing between screens
Dependencies in build.gradle(Module:)
implementation “androidx.navigation:navigation-fragment-ktx:$navigationVersion”
implementation “androidx.navigation:navigation-ui-ktx:$navigationVersion”
In single-activity architecture, the main activity’s layout xml only needs one element with 8 attributes. List them.
^fragment xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns: app="http://schemas.android.com/apk/res-auto" android: id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android: layout_width="match_parent" android: layout_height="match_parent" app: defaultNavHost="true" app: navGraph="@navigation/nav_graph" /^
What objects can findNavController( ) be called on?
Fragment or View.
Both end up calling the function on the fragment’s root view.
What is the syntax for navigating in code using the navController WITHOUT Safe Args?
findNavController( ).navigate(R.id.my_action_id)
What attributes does a fragment need when it’s added to a nav_graph?
What optional attributes are included in Codelabs?
^fragment android:id="@+id/gameFragment" android:name="com.example.android.navigation.GameFragment" tools:layout="@layout/fragment_game" ^ ^action android:id="@+id/..." app:destination="@id/..."/^ ^/fragment^
OPTIONAL
fragment - android:label
action - app:popUpTo=”@id/…”
action - app:popUpToInclusive=”true/false”
If navController is required in the Activity, what is the syntax to access it?
Next, set up an action bar with a back button that uses the navController.
In Activity.onCreate( )
val navController = findNavController(R.id.myNavHostFragment)
To set up action bar with back button:
In Activity.onCreate( )
NavigationUI.setupActionBarWithNavController(this,navController)
After onCreate( ) override fun onSupportNavigateUp(): Boolean { val navController = this.findNavController(R.id.myNavHostFragment) return navController.navigateUp( ) }
How should the functionality of an app bar’s back button differ from the device’s back button?
App bar back button:
- navigates within the app based on nav_graph
- never takes user out of app
Device back button:
- navigates back through the back stack
What are two other names for a key-value store?
Dictionary
Associative array
What is the Safe Args feature?
Why are Safe Args more reliable than bundles?
The feature is a gradle plugin, a feature of Android’s Navigation Architecture Component, that generates code and classes to detect arg errors at compile time.
Type mismatch / missing key errors aren’t picked up by the compiler but will throw runtime errors
How to implement the Safe Args dependency?
In build.gradle(PROJECT) dependencies{ ... classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion" }
In build.gradle(App)
apply plugin: ‘androidx.navigation.safeargs’
What is the syntax for navigating in code using the navController WITH Safe Args?
view.findNavController().navigate(MyFragmentDirections.myNavActionId(myNavArgs))