Showing posts with label Network. Show all posts
Showing posts with label Network. Show all posts

Monday, June 8, 2015

Developing for Android VIII The Rules: User Interface

Hot off the press (do people even say that anymore), the latest installment from Google Developers - Developing for Android VIII The Rules: User Interface.

tl;dr : Some important UI details and patterns as they pertain to user experience and performance (which are related).


  • Avoid Overdraw 
    • Enable Debug GPU overdraw in developer settings
    • Avoid opaque views covering other views
  • Avoid Null Window Backgrounds (had not thought of this much before)
    • Eliminating can be valid, but introduces new issues trying to solve overdraw.
    • Instead of setting background in manifest, set background in onCreate() by doing getWindow().setBackground(null) - warning, this can cause issues.
    • The correct way is to use the starting window with the background you want!
  • Avoid disabling Starting window
    • This causes the slow start time of the app. 
    • use a custom drawable if necessary to customize the starting window
  • Allow Easy Exit from Immersive Mode
    • Unless in a game, make it easy to leave immersive mode by tapping. 
    • Check the developer guide (link in the article)
  • Set Correct Status/Navigation Bar Colors in Starting Window
    • Theme the Starting window as you would your app.
      • android:statusBarColor
      • android:navigationBarColor
    • For the activity you can modify them via onCreate
      • getWindow().setStatusBarColor()
      • getWindow().setNavigationBarColor()
  • Use the Appropriate Context
    • For UI context, use activity context, not application.
    • For resources for your activity, use activity context.
  • Avoid View-Related References in Async Callbacks
    • Avoid referencing View/Activity/Fragment in network operation / long running async task. 
    • Consider using weak callbacks.. 
    • It also says consider an event bus, but I personally don't like the event bus because it does not honor the application lifecycle, and you must program around corner cases. (more on this later probably)
  • Design for RTL
    • As of API 17, applications should use start/end instead of left/right
    • You can enable RTL in Developer options
  • Cache Data Locally
    • "Cache Locally, Sync Globally"
    • You should create a local db that keeps data in a meaningful way (not just serialized server response).
    • Update your UI from local data whenever possible
    • Google I/O 2010 has video, Android REST Client Applicaitons. And personally once again, I would avoid the recommendation of an EventBus. Not even needed if you read your data from a db.
  • Cache User Input Locally.
    • Cache any user input locally, then send it to the server, this avoids it getting lost.. 
    • Have some UI indication that the data is pending vs. complete.
  • Separate Network and Disk Background Operations
    • Have two different thread pools one for network one for disk

Some of these things I was not aware of, partially because I had never done some of the 'don't do this' things.. Others I actively do (Syncing data), and other things were totally new, and a great idea, such as having two different thread pools.

Thursday, June 4, 2015

Developing for Android, IV: The Rules: Networking

Google developers are at it again with part 4 in their who knows how many part series with Developing for Android, IV: The Rules: Networking

tl;dr: One of the largest consumers of battery is network / data transmission.
  • Be stingy when and how often you get your data. 
  • Use tools like GCM, JobScheduler/GCM Network Manager. 
  • Don't ever poll. 
  • Don't sync everything.
  • Don't assume the network is up
  • Use exponential backoff when requests fail
  • Develop for the low end network - slow / packetloss. 2G networks
  • Create mobile specific APIs which return the data that the user needs. 
Expanding on these bullet points, I think one which is overlooked the most, and probably requires the most work, is having mobile specific APIs. It may be fine for your website to make 5 different requests to look up data to render on a page, but if you have a native app, tailor the API to a single call, and only return the data that is needed by the client. I don't know how many times I have seen APIs where I have to request a second API to get some little bit of data that could have been included in the first, but I have to download a 80KB json file, and parse it just to get the single field. The other recommendations are fine, and can usually be handled by the mobile engineer. Getting the resources to customize / write new APIs for mobile can be a much larger challenge. Expend your engery on it, it will make your life much easier down the line, and make your users much happier.