(bright music)
Okay, so we need to remove the up button
from the toolbar.
We could do that in undetach
but I think it's safer to let main activity handle this.
It's fine to have a fragment at the up button
because it knows that it's needed.
But there are priority at beam one.
In that case, we don't want a fragment to go removing it.
The fragment's got no idea what else the activity is doing
nor whether its activity was navigated to
from another activity.
If our fragment removes the up button,
it may break the navigation back from its own activity.
Main activity already removes the fragment
and that's a good place to also remove the up button.
So let's have a look at doing that
in the removeEditPane function.
We'll do that as the last statement in that function.
I'm gonna type supportActionBar
question mark dot setdisplayHomeAsUpEnabled false.
Now remember that we have to use the supportActionBar here.
If you use ActionBar instead, the code will still run
but the button won't be removed.
Our app doesn't have an ActionBar.
It's using the support ActionBar from the support library.
Alright, so that code on line 62, will remove the up button.
We just now need to call this function
when the button's tapped.
And we do that just like we respond
to any of the other toolbar icons in the on options,
item selected function.
So let's just go down and check that out.
Then let's add some code.
We'll have it starting on line 85.
So that's gonna be android
.R.id.home
and dot period, when I say that it means the same thing
in case I haven't mentioned that before.
Or any of that dash, greater than sign again.
Adding a block of code there, left and right curly braces.
We'll stop by logging, so log.d
parenthesis tag comma onOptionsItemSelected
colon home button pressed.
Next line, val fragment is equal to supportFragmentManager
dot findFragmentById.
In parenthesis it's gonna be R.id.
and its task_details_container, closing parenthesis.
Then the next line is gonna be removeEdit fragment, fragment
and that's obviously calling the function that we just added
the entry to remove the up button.
The ID R.id.menumain_addTask
which you can see on line 83.
That's an ID we've used in our layout.
The ID for the up button is provided by android,
android.R.id.home.
So we respond to it being tapped
just like any other menu item and we're then using
our removeEditPane function to remove the fragment.
I've locked the home button being pressed
because that's what it's called inside android
as the ID indicates.
This can be confusing because the phone
also has a home button that returns to the home screen.
While I'm in main activity, there's a couple typos to fix.
We did put a banner message up in an earlier video.
So you probably have already corrected them.
But it's in the onCreate function
towards the top of the class.
We need to fix this because I've got onCreated finished,
it should be onCreate starts.
Okay, and there's a couple of warnings here
to fix as well.
Well, there's actually one in this particular function.
You can see you've got a reference to var there,
that should be declared as val
or can be declared as val instead of var.
That will fix that warning.
Alright, so that's so many to fix there.
The second one was in our onSaveClicked function.
I've also used a var for that.
We can use a val for that, as the warning indicates.
We'll do that, and that fixes that warning up as well.
Alright, time now to see if it works.
So before I do that, we need to go into our
Android Virtual Devices.
We need to create a new emulator for API 28.
I'm gonna select Nexus 5X again
because that's what I've used previously.
I'm gonna make sure I select the pie, API level 28.
So using the latest version.
That's where our code is building against now,
that's the version we've specified
in the build.greater file.
Click on Finish.
That's our API 28.
And it didn't really matter whether we chose the Google Play
or the other version the Google API version.
I'm just gonna use the play version for now
and will kinda start that.
You can close down this window.
And that boot up.
I'll make that a bit bigger so we can see what we're doing.
So there's our emulator started up.
Let's actually run the app
on the API 28 device.
Alright, so the app's working okay.
Let's click on the little plus icon
and you wanna be in the portrait view for this,
which we are.
The up button appears in the toolbar as you can see
and we're tapping it back now.
As I'm tapping the up button,
successfully takes us back to the main screen.
Notice now that the up button is removed as well
so that's good.
We're gonna tap the button again now
and this time navigate over into landscape.
You need to tap this little button down here
which then actually causes the emulator
to go into landscape mode.
Now the words, it's no longer updating
the screen automatically which is a bit annoying.
But one of the things you can do to fix that
is you can drag down the settings
and click on this little icon here,
the square arrowed icons.
You click on that, that goes back to changing automatically
which is what we want here.
So I'll get rid of that.
And just to confirm that, I go back to portrait,
back to landscape and it's now moving for us,
moving to a landscape mode automatically for us.
Again, we've got at the moment now that we've
had moved into landscape mode.
We've still got the up button showing in the top,
upper left-hand corner which is good.
And tapping it, removes the fragment which is good.
So that's working and clicking on that,
one more time the plus icon,
and up, definitely closes the fragment
so that's actually working well.
Let's finish the video here.
In the next one, we'll have a look at what we should do
when the back button is pressed as opposed to the up button
that we've been working on.
I'll see you in the next video.