‫In this video.
‫We are going to continue with the code cleaning process.
‫Hello my friend, and welcome to Master Coding Channel in the complete Android Developer course.
‫And in this video we will continue decoupling the codes from the main UI.
‫So what we need to to do also we have seen in the previous video that we have decoupled the UI and the
‫networking sections and networking codes.
‫In this video I will show you how to decouple the fragment manager.
‫So this fragment manager, this dialogue server error dialogue fragment will be shown for the user when
‫the user has no internet.
‫So.
‫This.
‫The the one problem is that this dialogue is shared between the two the activities, the details activities
‫and the list activity.
‫So.
‫We need.
‫If we scroll down, we we will see that the fragment manager is also found here.
‫But what is the problem?
‫So why I can keep I can keep these two lines, why I should decouple and make a decoupling of the logic
‫and separation of the codes.
‫This is why.
‫So run the application and I will show you what is the problem.
‫This is our application.
‫So if I, I if I disable the internet connection, I will disable it.
‫I will close the application and it's working fine.
‫And I run it again.
‫It will display for me the dialogue.
‫This is the cached, this is cached, it's caching the data.
‫So if I run the application, the I.
‫It's not caching.
‫It is.
‫Exactly.
‫I need to turn off awesome by data.
‫And exit all everything.
‫Now run the application.
‫It should show us the server dialogue.
‫But what is the problem?
‫So if I if the user quit this and reopen the application, it will display this dialog.
‫Click okay.
‫And another dialog under the first dialog will be shown.
‫And this is a very, very common bug.
‫This is a coding bug, so we need to remove this.
‫So the user should deal with only one error, only one error of dialogs.
‫So if I run the application it will display one.
‫If I quit it and run the application again, it will display the two.
‫So I need to remove this error.
‫I will close the emulator.
‫And I need to make a sharing this dialogue between all activities.
‫So extracting the dialogue manager logic into a standalone class.
‫For that, I need to create a new I need to to order and organize my code.
‫So first of all, let me create I will close this.
‫I will create the package.
‫Called Common.
‫And I will put.
‫I will move the constants to the common refactoring and the server dial error dialog fragment to the
‫common package.
‫So.
‫I am organizing the code.
‫In order to to to understand that these are common between all.
‫Activities.
‫So I will create a new Java class.
‫And as I told you, I need to make a dialogs manager.
‫Manager.
‫This is a stand alone class that I need to.
‫Sure between that, the activities, the different activities between the list and the details activity.
‫So whenever a dialogue is shown with a non-empty ID, the provided ID will be stored.
‫Argument bundle.
‫Under this key, I will create a key here.
‫Public.
‫Sorry.
‫Public static final.
‫String and arg.
‫Argh!
‫You meant dialogue.
‫ID, So this ID, as I told you, it is an argument ID or.
‫You meant dialogue.
‫ID?
‫Okay.
‫Now in case activity or fragment that instantiated this dialogs manager are re created.
‫Okay so I need to make this object should be used in activities and fragments in order to manage dialog.
‫So I will annotate it as UI thread.
‫Okay.
‫And it is imported in the package and I need to create also a public private string final.
‫Public staff Private string dialogue fragment tag.
‫Okay, I will copy and paste and post the video in order to make this video more and more faster.
‫And we concentrate on the concepts, not on the codes.
‫So I will also create the two private fragment manager and dialogue fragment.
‫Okay, I will import it and dialogue fragment also imported the classes so we are ready to start making
‫our constructor.
‫So I will click alt plus insert the constructor and I need to make the fragment manager.
‫So first one is the fragment manager and what we should do with this fragment manager.
‫The fragment manager we go to the.
‫Questions list activity and we see that the fragment manager here is the same and we need to cut it.
‫I need to cut the code from here and I will annotate like this.
‫We have got it.
‫Go from here.
‫We have cut code from here.
‫Okay.
‫So I will paste it here.
‫And this needs an argument to be passed.
‫So I will pass dialogue, frame and tag.
‫Okay.
‫And I will not use this get support I get.
‫I need to make fragment Manager This fragment manager.
‫M fragment manager dot find the fragment by ID and I'm passing the ID of that fragment, which is the
‫dialogue fragment.
‫Okay, so it's very simple change variable type to fragment manager.
‫Okay.
‫In dialogue fragment tag.
‫Okay.
‫This is should be not fragment manager.
‫It should be the fragment it is telling us with this error.
‫So I need to make it as fragment.
‫Okay.
‫And I will remove this.
‫Fragment, I will import the class.
‫And if we start here by testing.
‫So there might be some dialogue already shown.
‫If fragment manager fragment with dialogue, this is a fragment manager.
‫I will make it as fragment with.
‫Dialogue tag.
‫Okay.
‫In order to understand what art is.
‫So if fragment tag does not equal to null.
‫I will go to make.
‫The logic here and I need to make also some tests.
‫So.
‫And dialogue fragment.
‫Fragment dot glass dot is assignable form or from.
‫And I need to pass the fragment.
‫Fragment with dialogue tag.
‫This is the fragment dialogue with tag.
‫Dot get class.
‫So what we are doing, what we are doing is making here the test.
‫If it is having a ready shown dialog.
‫Okay.
‫So I am currently shown dialog equal to.
‫Dialogue fragment disgusted by this and fragment with dialogue tag.
‫And it is good to go.
‫Okay.
‫It's very simple.
‫And these are the strings and the private dialogue managers.
‫Okay.
‫Now I need to make also dialogue fragment here.
‫Nullable.
‫Nullable and I need to make public dialogue fragment.
‫Also the constructor.
‫Get.
‫Current currently showing dialogue.
‫This is method and I will return.
‫Currently showing dialogue.
‫Okay.
‫This is we are this is the dialogue that it's shown and it's currently shown.
‫I need to copy and paste.
‫Another method is called get currently shown dialogue ID and I am passing if currently and then I'm
‫testing if currently shown dialogue equal equal to null and get arguments equal to null.
‫So it will not display and it will return the the empty string.
‫Otherwise it will get the string of argument dialogue id.
‫Okay.
‫I need to copy and paste.
‫Also, one method is called is dialog currently shown.
‫I will import the text utils and we are good to go.
‫Also, I need to make another method this currently shown dialog which is that suggests and that make
‫obligatory to to close and force closing of any dialogs that we have already displayed.
‫Okay.
‫Also show written dialogue with ID.
‫I will need to pass four parameters dialogue fragment and the nullable string.
‫ID dismiss currently shown dialog.
‫This is the method that it's called and dialogue set retain instance.
‫Set ID and show dialog.
‫These are the two methods, the last two methods that I need to create in this class and I will pasted
‫them here.
‫So set ID would have the bundle.
‫I will import the package, import class and get arguments and it is if it is equal to does not equal
‫to null.
‫Otherwise go and get a new bundle.
‫Make new bundle which is the capacity of it.
‫It is equal to one.
‫The arguments that put the string, put the string of arguments, dialogue and pass the ID and dialogue,
‫set arguments to args show dialogue.
‫These are the showing of the dialogue.
‫Okay, It's very simple.
‫This is our class that we have created and.
‫And I have missed nothing.
‫It is equal to tag.
‫Okay.
‫And I'm passing the dialogue fragment tag.
‫And this is the created the.
‫Class.
‫It's called Dialogues Manager.
‫So how to use this?
‫Dialogues Fragments.
‫We go back to the main activities and the questions list activities, and we create an instance of this
‫class and we will use it in the place that we have moved and removed the.
‫The code.
‫Okay, so here.
‫For dialogue.
‫Fragments.
‫As I told you, private dialogues Fragment or fragment Manager.
‫Manager.
‫Equal dialogue.
‫Manager.
‫Okay.
‫It's good.
‫So this is the instance that we are going to use.
‫And I will scroll down to the Oncreate and I need to make here the dialogue manager and M dialogues.
‫Dialogue Manager Equal to new Dialogues Manager.
‫Dialogues manager and get supported fragment manager.
‫Okay.
‫That passed as parameter.
‫Okay.
‫It's very simple guys.
‫And where we got and where we have got it, the code we scroll down to on fetch questions on fetch of
‫questions failed.
‫And we need to pass and use the.
‫And they look okay.
‫So I have cut the code from here.
‫I will replace it with M dialogues Manager Dot show retained dialogue with ID and I will pass server
‫error dialogue fragment with new instance.
‫New instance and I will pass the ID as an empty string.
‫Okay.
‫It's very simple.
‫What I have made.
‫I have created a standalone class.
‫It's called Dialogues Manager.
‫And this dialog manager allows me to use it for different different activities without making redundancy
‫of the code and repeating of the code.
‫So again, I need to go to the questions, questions, details, activity.
‫So questions list.
‫And questions.
‫Details.
‫Questions.
‫Details.
‫First questions.
‫Details.
‫Details, questions.
‫So questions, detail, activity.
‫And in the same way, I need to decouple.
‫The.
‫The already made that looks so Questions detailed activity list the activity.
‫It is done and I need two questions to make.
‫The same with the questions.
‫Here.
‫I need to create a private dialogue manager.
‫Dialogues.
‫Manager and dialogue.
‫Manager.
‫And I will scroll down to the Oncreate.
‫On create.
‫This is here dialogue.
‫Error.
‫I will create dialogue for a manager, equal new dialogue manager and I will pass get supported Fragment
‫manager.
‫Okay.
‫So in this case.
‫I need to decouple the code of the error.
‫So I will remove this and I will put an dialogs manager dot show, retain dialog with with ID and I
‫will pass the server error the the class that I need to create from it the dialog and I will pass the
‫ID as an empty string.
‫So let's save our project and let's run our application and here is our application.
‫It is displaying for us the server error.
‫Box.
‫And if I run the application again, it will display me one.
‫Dialogue.
‫Okay, So thank you, guys.
‫I hope you benefit from our video and we are now ready to start and we are making clean architecture
‫in order to understand the dependency injection and start using dagger for dependency injection.
‫So I hope you benefit from our videos.
‫I hope you rate us five stars on Udemy and support us by subscribing to our YouTube channel.
‫Thank you guys.
‫See you in next videos.
‫Next videos.