-
Notifications
You must be signed in to change notification settings - Fork 0
Lab 5
In this lab, we will setup a basic navigation infrastructure for a new Study app
-
Create a new Android Studio project named
Android Study Guide
-
Ensure you have an empty Kotlin
Activity
namedMainActivity
- This should be set as the app's launcher activity in
AndroidManifest.xml
- This should be set as the app's launcher activity in
-
Create a second Kotlin
Activity
namedTwitterActivity
- This will be used later this week to integrate with Twitter's PWA
-
Create 4 Fragments that will become the core screens in our app
CreateNoteFragment
NoteDetailFragment
MyNotesFragment
StudyGuideFragment
-
For each
Activity
&Fragment
add aTextView
to the layout that uniquely identifies that screen -
Add a
Button
toMainActivity
that showsTwitterActivity
when clicked -
Add 4
Buttons
toMainActivity
that show each of the newFragments
when clicked- To show a
Fragment
, you'll need to add aFrameLayout
toactivity_main.xml
to act as theFragment
container. - Give the
FrameLayout
an id offragmentContainer
- Make the
FrameLayout
fill the layout. - Give the
FrameLayout
an elevation of4dp
.- This is just temporary to ensure the buttons aren't drawn above the container. We'll replace this in the next lab.
- Add the 4
Buttons
and give each a label to indicate whichFragment
they will show. - Add click handlers to each button within
MainActivity
- Within each click handler, perform a
replace()
FragmentTransaction
to show the desiredFragment
- To show a
Using Logcat
, add log messages to key lifecycle events in an Activity
and Fragment
to observe lifecycle changes as you interact with your app.
Temporarily change your app's launcher Activity
to TwitterActivity
and observe the impact on your app
- How to Create a Fragment?
- TextView Documentation
- Button Documentation
- Material Design Button Guidance
Android Studio can help us create a new Activity
or Fragment
.
Right-click on your main package within src/main
, select New
-> Activity
/Fragment
and select the template you want to use.
However, the IDE often presents us with a handful of different templates such as EmptyActivity
, Fragment with ViewModel
, List Activity
, etc
These can be kind of fun to play with when starting out, but more most situations you're going to want to choose EmptyActivity
and BlankFragment
so you can customize exactly how you want without having to remove stubbed out code.
To show a Fragment
we need 2 things:
- We need a
ViewGroup
to act as a container for theFragment
. In this case, we could use aFrameLayout
that fillsactivity_main.xml
. - We need to use the
FragmentManager
to createFragmentTransactions
that add/removeFragments
from the container.
Here is a sample method that you can add to MainActivity
to allow for showing Fragments
within a container with an id of fragmentContainer
.
private fun showFragment(fragment: Fragment, name: String) {
supportFragmentManager
.beginTransaction()
.replace(R.id.fragmentContainer, fragment)
.addToBackStack(name)
.commit()
}
Intents are a very general-purpose tool in Android. We can create them in a variety of ways to achieve a desired result.
In this case, we want to create an "explicit Intent" to open a specific Activity
- our TwitterActivity
.
To do that, we need 2 things:
- a
Context
. This is usually going to be an instance of anActivity
. - the class name for the
Activity
we want to start.
Once we've created an Intent
, we can use Activity.startActivity(intent)
to start the desired Activity
binding.showTwitterButton.setOnClickListener {
val showTwitterIntent = Intent(this@MainActivity, TwitterActivity::class.java)
startActivity(showTwitterIntent)
}
The Android SDK provides a default set of logging functions:
Log.d()
Log.e()
Log.i()
- etc
Adding these to your code can be a helpful way to observe the lifecycle of your application or to help track down bugs.
Output from these functions will go to Logcat
which can be observed in the Locat
tool window or using adb