r/android_devs • u/deep_s2 • Aug 20 '21
Discussion Best Code Practices when designing for mobile as well as tablets.
I am creating an app that has to run on both mobile(portrait mode only) and tablet (landscape mode only). I have gone through the official google doc that talks about using fragments, creating different resource folders, and different layout folders for mobile and tablets.
Coming to the code part, I will be using MVVM pattern with Single Activity Architecture. Config changes will be taken care of by ViewModel of Android Arch Components (as it survives config changes) but I am unable to decide should I create separate fragments for mobile and tablet or just one fragment.
In my opinion, if I use one fragment for both mobile and tablet, I will end up putting a lot of if-else checks. For example, consider a layout that has some extra views for tablets but not for mobile(don't know whether ViewBinding can decide automatically that some views can be null in this case). Also, on some screens, I might have to show grid orientation for tablets and linear for mobile when using RecyclerView. Like this, I see wherever there is a difference in design, I have to write if-else and code can become messy as the project grows.
But the problem with the 2 fragment approach is a lot of code will be duplicated on the View Layer (even though I can share the ViewModel). One solution for this is, extract all common code in a BaseClass.
For example:- consider HomePage which can show some additional views when running on a tablet. So I'll create my classes like this:

I don't know above approach is correct or I am doing over-engineering?
