Activities and Fragments Flashcards

1
Q

Why extend AppCompatActivity?

A

AppCompatActivity is a subclass of Activity that supports all modern Android features while providing backward compatibility with older versions of Android.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does AppCompatActivity.setContentView( ) do?

A

Specifies AND inflates the layout xml associated with an Activity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the name of the bar featuring clock, battery, signal etc

A

Status bar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between the following?
ActionBar
AppBar
Toolbar

A

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/^

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the navigation component enable?

Which 2 dependencies are required to use the navigation component?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In single-activity architecture, the main activity’s layout xml only needs one element with 8 attributes. List them.

A

^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" /^
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What objects can findNavController( ) be called on?

A

Fragment or View.

Both end up calling the function on the fragment’s root view.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the syntax for navigating in code using the navController WITHOUT Safe Args?

A

findNavController( ).navigate(R.id.my_action_id)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What attributes does a fragment need when it’s added to a nav_graph?

What optional attributes are included in Codelabs?

A
^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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How should the functionality of an app bar’s back button differ from the device’s back button?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are two other names for a key-value store?

A

Dictionary

Associative array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the Safe Args feature?

Why are Safe Args more reliable than bundles?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to implement the Safe Args dependency?

A
In build.gradle(PROJECT)
dependencies{
...
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
}

In build.gradle(App)
apply plugin: ‘androidx.navigation.safeargs’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the syntax for navigating in code using the navController WITH Safe Args?

A

view.findNavController().navigate(MyFragmentDirections.myNavActionId(myNavArgs))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the syntax for receiving navArgs in the receiving Fragment? (2 versions)

A

val args = ReceivingFragmentArgs.fromBundle(requireArguments( ))

OR

val args: ReceivingFragmentArgs by navArgs()

17
Q

What is the difference between an explicit and an implicit intent?

A

Explicit
- send a message to a specific target

Implicit

  • initiates an Activity without knowing which app or Activity will handle the task.
  • requires an ACTION (eg ACTION_VIEW, ACTION_EDIT etc)
18
Q

How to build an intent in code?

A
private fun buildIntent( ): Intent {
val intent = Intent(Intent.ACTION_*)
   intent.setType("mime_type")
            .putExtra(Intent.EXTRA_*, extraParams)
   return intent
}

startActivity(buildIntent( ))

// Build and return the intent from a function so it can be checked if needed before being used to start an Activity

19
Q

How to check if an intent will resolve before using it to start an Activity?

A
if(intent.resolveActivity(requireActivity( ).packageManager) == null) {
  // handle intent not resolving
}
20
Q

How do you make it possible for an instance of a data class to be passed as an argument from one fragment to another?

A
Make it parcelable (recommended, faster)
Annotate the data class with @Parcelize
Have the data class implement the Parcelable interface
Make it serializable
Have the data class implement the Serializable interface
21
Q

What is the difference between a Bundle and a PersistableBundle?

A

A persistable bundle is more restrictive (can only contain basic Types) but is disk safe (more reliable). Apparently useful when dealing with Job Services and Job Schedulers.