(vibrant music)
(typing)
Alright so we've got the app navigation working.
Let's have a look at what we're gonna do
when the back button's tapped.
Reacting to the back button's really easy.
Activities have an onBackPressed function that gets called.
What we have to do to start with is decide how
our app's going to respond to it.
So let's get back to the app
and start by seeing what happens at the moment,
when we just rely on the default behaviour.
So we're gonna bring up the emulator again.
And you can see the emulator's in landscape.
So we're gonna start there.
So we'll just start the app again.
It's been backed up.
So if I tap the back button now, from the main screen,
you can see it's just closed the app,
which is exactly what the user would expect to happen.
And the same will happen in portrait.
And that's fine.
So we'll run the app again now.
This time I'm gonna click on the plus icon.
Now we've got a different situation.
What will the user expect back to do here?
If they'd added some data,
the very least we should do is ask
for confirmation that they wanna quit editing.
And we'll be looking at dialogues later
and we'll see how to do that.
If they choose to quit, should we exit the app
or just remove the fragment?
I personally probably want the app to exit if I was
in this position as a user, so that's what we'll do.
The situation's different in portrait though.
We still got the fragment showing.
So I rotate the device back into portrait now.
So this is a bit different.
As a user, I'd expect the back button
to do the same as up from here.
In other words, take me back to the main screen.
That seems more expected, so that's what we'll do.
Notice, if I press back at the moment,
it just exits the app.
Right, so let's get back to main activity.
And what we need to do is add the onBackPressed function
to main activity.
And I'll do that towards the end of the file.
Just before these life cycle functions,
so I'll just do it down here.
So let's get to override a method.
And the method we wanna override was onBackPressed.
So I've grabbed that one.
We entered the default code for it.
Alright, and it's that code
to the super dock onBackPressed function,
that's causing that default behaviour.
So basically calls another function,
the activity's finished function to close down the activity.
We're gonna check to see if the added at fragment exists
and changed the behaviour if it does.
So let's start with some code.
So I'll start by val fragment
equals supportFragmentManager
period findFragmentById parenthesis R dot ID dot
task_details_container, which is our edited fragment.
And then we'll do a test.
If fragment is equal to null
or bring nTwoPane, nTwoPane mode, then and only then
will we call the super to onBackPressed.
Otherwise, we put an else here.
What we'll do instead is we'll removeEditPane
and pass our fragment to that.
So if we're not showing the fragment or
if we're in twoPane mode then we're calling the super dock
onBackPressed function to exit normally.
Otherwise, we're just removing the fragment
using our removeEditPane function.
So as I said we'll be adding a dialogue later
to get confirmation that the user's happy to lose any
changes there that you may have made when editing.
For now though, see how this works.
We'll run the app again.
And so my emulator's in portrait,
so I'll start there.
So I'm gonna click on the plus icon to display the fragment,
then we'll click on the back button.
So that now looks like what users will expect.
The app just returns to the main screen.
And if I press back again, the app exits normally.
Alright, so let's run the app again.
We're gonna tap the plus icon
and we're gonna rotate it to landscape.
Because the main screen is showing the user,
we'll probably expect to close the app.
Of course you might disagree with this, and that's fine.
If you want the screen to behave the same as portrait
and just remove the fragment,
then you can just remove the task for nTwoPane
in our onBackPressed function.
It's up to you how your app behaves.
I'm sure you had achieved whatever you decide.
Alright so let's tap back now to see what happens.
And in our case, because of the way we've coded it,
the app exits.
And we should also make sure that
we haven't broken the app button.
So let's just run the app again.
And tap on plus again.
So the up button here in landscape doesn't quit,
it just removes the fragment.
Well it does what it should do, so let's confirm that.
That's correct, it has done that.
And let's go back to portrait.
Plus, click on up, and it successfully removes the fragment
and it doesn't try and exit.
So up will remove the fragment as you saw
and there's no further testing needed and
that's because the up button's been removed from the toolbar
in both landscape and portrait testing there.
Alright so that's our up and back navigation
finished for now.
And we'll be looking at dialogues later,
as I mentioned and giving the user a chance to confirm
that they wanna quit but I'll stop this video here for now.
The next step in our app is to save the data
that the user's entered or changed.
So it's time to start hitting the database.
As I mentioned in the view model section,
Google have now deprecated the loader classes.
We'd originally used the loader to provide data
for our main activity.
Now we're going to use it to view model instead.
When writing your apps, you'll often get to a point
where something you wanna do relies
on something you haven't written yet
and that's the case here.
Our view model class will take care of presenting the data
to main activity as well as saving the changes.
But we haven't written that yet.
Obviously we can't present data to display
if there isn't any saved in the database.
So they get around that.
I'm going to get the edited fragment to save the data.
This is only temporary.
And we'll be moving the code into the view model
but it's quite common to do things like this.
If we put the code that saves the task into its own function
moving it shouldn't result in not too many changes
when we get to that stage of moving it.
So that's what we'll do
and we'll start work on that in the next video.