r/androiddev • • Sep 16 '18

Why does Android development feel like hell?

[deleted] — view deleted post

207 Upvotes

174 comments sorted by

View all comments

108

u/nhaarman Sep 16 '18

Because there is no proper separation of concerns provided by the framework. By default, everything is entangled: View handling, lifecycle management, navigation, business logic, etc.
Current solutions try to separate some of these concerns by building solutions on top of messy foundations (like the Activity or FragmentManager), but a lot of the Android quirks still leak through into your business logic.

A question seen a lot lately is, "How to do bottom view navigation?", which are two completely separate problems which, when seen separately, aren't real problems: you have a UI element that are basically just buttons, and a navigational element that handled navigation. However with Android, you are almost forced to deal with them in a completely coupled manner.


Instead, it's time to take back control over your application and say goodbye to the entangled coupled mess that Android brings. Instead of building solutions on top of the messy Android foundations, build a stable Android-agnostic application that handles business logic and navigational logic as completely separated concerns.
Then, plug in the Android framework as the UI layer into your application. Doing this will lead to a far better maintainable and testable application.

30

u/ssshhhhhhhhhhhhh Sep 16 '18

but seperation of concerns isnt actually going to make it less likely to spend 6 hours on navigation drawers and tree views without progressit'll help with maintenance but for actually putting something together, nope

11

u/nhaarman Sep 16 '18

Of course, that's still something you'll have to deal with. However, this 6 hours is only invested once due to decoupling instead of again later on when some business rule changes.

22

u/Foxtrot56 Sep 16 '18

Don't forget that these agnostic standards, ideas and frameworks change every 6 months.

9

u/nhaarman Sep 16 '18

That's because these solutions tend to build upon the Android framework instead of being completely agnostic. There are too many hacks involved to build solutions in top of the framework that don't work well, leading to a new solution with different hacks.

5

u/Foxtrot56 Sep 16 '18

Right and the totally agnostic ones crash and burn all the time because Android implements a new paradigm and then 6-12 months later this agnostic thing gets it and you are late to get it so you ditch the framework and start implementing Android again.

There isn't a good solution for this churn except to not rely on third party frameworks. You either totally rewrite the parts of your app that these frameworks touched or you implement native solutions yourself. The shelf life of third party android frameworks and libraries is somewhere around two years.

6

u/nhaarman Sep 16 '18

What paradigm would this be? A totally agnostic solution is not affected by 'new Android paradigms'.

5

u/Foxtrot56 Sep 16 '18

Implementing new features. Something like react is really slow to offer support for these things. Flutter is better because you can always add it yourself fairly easily but if it is heavily tied to the android system then it's much harder. Adding new stuff with motionlayout is impossible in react, probably possible in flutter and really easy in Android.

3

u/nhaarman Sep 16 '18

Don't forget that these agnostic standards, ideas and frameworks change every 6 months.

Adding new stuff with motionlayout is impossible in react

Then React isn't really Android agnostic, right?

→ More replies (3)

3

u/Zhuinden Sep 18 '18

the totally agnostic ones crash and burn all the time

The totally agnostic ones crash and burn because they ignore state persistence so they can only be used to make unstable crappy apps that forget what I input into textfields and stuff :(

→ More replies (7)

8

u/BlackVale Sep 16 '18

I’m new to android development and don’t understand what you mean by android-agnostic. Do you mean create a basic Java app with business logic and slap an android UI on top of that?

14

u/nhaarman Sep 16 '18

Ideally, yes. You'd create a Java application that takes in consideration everything vital from a mobile viewpoint: screens, lifecycles, state saving/restoration and navigation. Nothing in this mentions anything Android related. Unfortunately this is not really trivial yet, as coupling Android to your app involves some reasonable amount of code to untangle the different responsibilities in the Android framework. Especially for a beginner, this is not feasible.

What you can (and should do) is at least decouple your business logic from the framework, and let Android be responsible for the presentation layer (UI, presentation and navigation). That way at least your business logic can be fully tested without having quirks from Android getting in the way.

2

u/SluggishJuggernaut Sep 17 '18

Any good tutorials you can recommend on doing what you mentioned there near the end?

→ More replies (1)

5

u/APimpNamedAPimpNamed Sep 16 '18

So build an Elm app that works under android UI?

12

u/nhaarman Sep 16 '18 edited Sep 16 '18

If you'd get that working, sure.

All kidding aside, you build a mobile application that works 'under Android ui'. You still have screens, you still have lifecycles, and you still have state restoration. However, you can build this completely android agnostic and in a proper separated manner so weird quirks the Android framework brings (destroyed activities on orientation changes, the back stack, the FragmentManager) don't cause changes in your core logic.

→ More replies (2)

3

u/lllama Sep 17 '18

Because there is no proper separation of concerns provided by the framework. By default, everything is entangled: View handling, lifecycle management, navigation, business logic, etc.

The whole SDK design is based around the idea that you do simple actions in your app, split up into small sections, that are persisted once you've done something important.

This type of stateless design is what the lifecycle of the UI is designed around. If followed properly this can lead to a very loose design, even if it's not the (by now) classic separation of view and state.

Then there is the Service pattern for longer running actions. In principle this barely communicates with the rest of the app. It should update the persistent state, then maybe signal the UI with an intent (if the UI is not just subscribed on persisted state, e.g. through SharedPreferences).

If you go back to 0.9 this is how things actually worked in apps.

It turns out this is not how people like to program. Almost everyone wants to build big in-ram state machines and do a bunch of one off things like network request, rather than e.g. long running synchronization to persistent state.

I'm not saying one method is better than the other, or that even if you follow this "classic" method you get a 100% proper separation of concerns, but it does enable a better separation of business logic itself compared to having bunches of stateful instances coupled together in RAM (and as an aside it does give you offline support almost for free).

However it does not really matter. Before Cupcake ASyncTask was a third party library which does pretty much everything you're not supposed to follow this pattern, but they threw it into the SDK. In some worse off timeline they would have kept on doing that with every library du jour. The paradigm died almost immediately, inside and outside of Google equally. Also it would probably not meet the needs of all the types of apps that exist today.

→ More replies (6)

7

u/ArmoredPancake Sep 16 '18

The hell is difficult with bottom navigation?

24

u/Zhuinden Sep 16 '18 edited Feb 08 '19

I mean, if you do Material Design 2.0 where

Behavior

Bottom navigation actions

Tapping a bottom navigation destination results in one of the following: Bottom navigation destinations don’t: The following guidance applies to Android only. Related Article arrow_downward

Tapping a bottom navigation destination results in one of the following:

  • It takes the user to the screen associated with it
  • On a visited section, it returns the user to their previous scroll position there
  • On the current section, it scrolls the page back to the top and may refresh it

Bottom navigation destinations don’t:

  • Open menus or dialogs

Tapping the navigation destination of a previously visited section returns the user to where they left off in that section.

Tapping the current bottom navigation destination takes the user to the top of the screen, and refreshes the content if applicable.

1.) you must retain the bottom navigation bar across the displayed views

2.) you must retain each tab's individual navigation history in its own backstack

Then that's actually quite tricky!

In fact it is so tricky that the Navigation AAC hasn't figured out how to do it yet with Fragments.

I think the only way to do it in a reasonable manner is with compound viewgroups and managing everything by hand.

Previously it said "if you click a different tab then clear the tab you switch away from" but now they decided to make dev life a tad bit more difficult.

EDIT from the future: hold on guys they've changed the Material Design 2.0 spec to make it suitable for Android Nav AAC, who saw THAT coming???

10

u/-ZeroStatic- Sep 16 '18

Bottom nav view + a manager that maintains stacks per tab. Plug in a framelayout on top that hosts the fragments and just replace fragments all day long.

https://github.com/ncapdevi/FragNav

Fragnav uses a similar method. (Or rather, it allows you to do it that way)

→ More replies (4)

4

u/aaulia Sep 16 '18

I'm doing stuff with bottom navigation and conductor, which is kind of nice, but #2 catches me off guard, I sort of go with the design without thinking (lol), fortunately I talk with the designer to just lose the bottom navigation bar for anything other than the initial views. It should be doable with conductor's router/childRouter, but the deadline is coming.  
I'd imagine doing it with fragments would be a whole lot more painful.

3

u/Zhuinden Sep 16 '18

I talk with the designer to just lose the bottom navigation bar for anything other than the initial views.

That's also what we do on this project and it definitely simplifies things :D

Previously we could bargain that the tab state was lost when you switched away, because the material spec said so.

→ More replies (1)

2

u/Foxtrot56 Sep 16 '18

Why would you do 2? Just dump the history, it's absurd to keep it for each tab. You are introducing way too much confusing logic that the user will never keep track of.

Unless you mean that the order the user pressed the tab should be recorded so that back goes to each tab they visited. I also do not agree with doing that but it is easier.

→ More replies (7)
→ More replies (4)

3

u/nhaarman Sep 16 '18

I don't know, and it shouldn't be, but it's an example of a question I've seen more than once.

→ More replies (2)

2

u/c0nnector Sep 17 '18

However with Android, you are almost forced to deal with them in a completely coupled manner.

That's because there's no concept of navigation.

They basically built a super abstract layer to handle core functionality but never took a stance on what is a mobile app and how it should operate.

I mean i get it, they gave you freedom to build your app as you see fit. By doing so they also throttled any real progress and gave developers a real bottleneck - reimplementing basic concepts and patterns.

→ More replies (1)
→ More replies (1)