So we've nearly completed the changes
to display our fragments side by side.
At the moment though, in main activity,
we go back to that now,
we've got this mTwoPane variable that's set to false.
So we need to change the code to correctly reflect
our device orientation.
So the first change we need to make
is in our onCreate function.
So let's go ahead and do that.
I'm gonna start by typing mTwoPane
is equal to resources dot configuration
dot orientation is equal to configuration,
this time with a capital C, dot orientation
underscore landscape.
So we're getting the current configuration
from the activities resource
then we check the orientation.
That's Configuration.ORIENTATION_LANDSCAPE,
the value that's returned, in other words.
Then that means we're running in landscape,
otherwise we're in portrait.
There's a bit more work to do there
if you're gonna do it to fully support large tablet displays
but that's enough to see how this all works.
Okay, so we now know the screen orientation.
The next step is to decide if we should showing in
AddEditFragment or not.
Remember that the activity gets destroyed
and recreated when the device is rotated.
If we were editing our task when that happens,
we wanna continue editing it after the rotation.
Android will reattach any fragments to our activity
automatically when it destroys and recreates the activity.
All we have to do is check if the fragment exists,
and adjust the display for a new orientation.
So let's go write the code for that.
Go ahead and do that.
We're gonna also create this in the onCreate function.
And underneath this line 2 get our fragment and
I'm just gonna copy that from our onSaveClicked function,
save a bit of typing, we'll paste that in there
into our onCreate function.
So finding our fragment, if it's not equal to null,
open the code block,
so firstly determined at this point,
if there was an existing to fragment to edit our task,
we then need to make sure the panes are set up correctly.
So let's add a comment to that effect.
Or are set rather set up set correctly.
To do that, we're gonna do task_details_container.visibility
equals View.VISIBLE,
then we wanna hide the left pane
if it's in single pane view,
click the comment to that effect.
Alright, so, we'll do a test for that.
So mainFragment.view safe call operator
dot visibility is equal to
if(mtwoPane) view.VISIBLE else View.GONE,
then outside of that, after the else,
after the test to see that fragment is not equal to null,
in other words if it is null,
put an else there, we're gonna put
task_details_container.visibility
equals and do another test here,
so if(mTWOPane), this time we're gonna do a
View.INVISIBLE else View.GONE.
And we also wanna add mainFragment.view safe call operator
dot visibility is equal to view.VISIBLE.
So hopefully, that should be pretty straightforward.
If the fragment exists, we need to show the frame layout.
We also need to hide the main fragment to the left hand side
in single pane mode.
If there's no fragment for adding or editing,
we need to hide the right hand frame layout
and make sure the main frame layout is visible.
We need to do a similar thing
in task edit request, our function.
We don't have to worry about the case
where our fragment's null though
because we're getting a new incidence of the fragment
and that's down in the task edit request function.
What we do need are the two lines with their comments
from inside this if block.
Rather than duplicating the code,
we're gonna cut those lines out of this onCreate function
and replace them with a call to a function
we're about to write called showEditPane,
So it's these three lines here,
I'm going to cut those
and then replace that with a call to showEditPane,
just the function we're about to write now.
Let's go ahead and write that.
That's gonna be private fun showEditPane
parentheses, I'm gonna paste in that code.
Now that I've done that, we can actually move down
to our task edit request method
and we can actually make a call there as well,
after the commit to showEditPane.
Alright, so that swaps our AddEditFragment in and out
when we add or edit a task
and make sure that the appropriate panes are visible
when they need to be.
When the device is rotated, we may already
have a fragment for editing
and that's why we've got the code in onCreate
that I've added up here.
So notice the slight difference
between using onCreate in the activity
and using it in the fragment.
Now here in the activity,
onCreate sets or calls rather, setContentView
to inflate the layout and you can see that code
on line 24.
Basically just after the super call there.
That means that we can access widgets
from the layout here, as long as we do it after the call
to setContentView, of course.
But over in the fragment, we go back to that again
to our AddEditFragment,
we can see that the layout here is inflated
in our onCreateView function on line 42.
That's basically inflated in the onCreateView function
not in onCreate.
Alright, so, we can now run the app to test that it works,
so let's go ahead and do that.
And I'm also gonna filter the Logcat,
so let's open the Logcat and filter it.
And I'm gonna filter it on AddEditFragment.
I'm gonna bring up the emulator
and we wanna make sure that that's in landscape mode,
which is currently set to it as you can see there.
So on the left hand side of the screen,
we've got our main fragment.
So that's where the list of tasks will appear
when we write the adaptor for the recycler view.
So when I come over here and click on the plus button
to add a new task, the AddEditFragment is added
to the frame layout and the frame set's now visible
as you can see there.
And moving that down, in the Logcat
we can see that the fragment function's been called.
So onAttach is called when the fragment's
attached to the activity,
then we can see it's onCreate,
and onCreateView functions are also called.
Remember that onCreateView is the function
that inflates the layout which is why we can access
the fragment's widgets in the onCreate function.
So, I'm gonna add a comment to the Logcat down here.
Portrait.
Then we're going to then rotate the emulator
back into portrait mode.
So that calls the activity to be destroyed
so the fragment detaches from the activity.
And you can see we got onDetach starts on this line here.
But at the moment, the fragment
also gets destroyed and recreated.
But we'll actually see a way to prevent that later.
For now though, both the activity and the fragment's
are destroyed then recreated.
So you can see there, the fragment's onAttach,
onCreate, and onCreateView function's being called again.
Alright, so our code to hide the main fragment
now seems to be working.
We can only see the details on the screen there
as you can see in portrait mode.
Before rotating again, I'm gonna enter some texts.
So, for task name I'm gonna go with fragment
and we're gonna use testing fragments for the description.
The Android framework takes care of restoring the contents
of the edit texts in the fragment,
just like it did for the activity in earlier sections,
so I'm gonna rotate back into landscape.
You can see that the text is still there.
So fragments receive a bundle when they're recreated.
They also have an onSaveInstantState function
just like activities do.
If we had other values for storing the bundle,
the process is exactly the same,
as we've seen, for an activity.
Alright, so we're in landscape at the moment.
The final thing to test is the fragment being removed.
Now, at the moment, that's all our save button does.
We haven't written the code to save those details yet.
So I'm gonna add another comment down here in the Logcat.
Saving.
And go back to our emulator
and I'm gonna tap on the save button.
So you saw that the fragment got removed from the screen,
and the Logcat shows that it was
detached from the activity as well.
We're going back to portrait now.
We need to do the same test for portrait mode.
So you wanna tap the plus button first to create a fragment.
Then, another comment there, saving again in Logcat.
Back to our emulator.
And I'm gonna tap on save.
You can see again that the fragment
got removed from the screen,
and also the Logcat has confirmed that it's been detached,
the fragment from the activity.
So that looks good, the fragment's removed,
and the main fragment widget becomes visible again.
We should repeat that test from rotating
from landscape to portrait,
to check that the main fragment is correctly displayed.
So let's go ahead and do that.
So I go back to landscape mode there.
Tap on the plus button to display the AddEditFragment.
Next, we wanna rotate back into portrait.
So that looks fine, we can still see that
the task details are showing on the screen there.
Tap on the save button and the fragment's removed.
That also appears to be working fine.
Alright, so let's stop the video here.
In the next one, we're gonna start looking
at the fragment lifecycle.
Once we've seen how that relates to the activity lifecycle,
we'll finish off the code to swap fragments,
by adding support for the back and up buttons.
So I'll see you in the next video.