android

android tips and tutorials

Android Room, database I/O, and Java 8 threads alvin August 7, 2017 - 10:50am

I just started working with the Android Room database persistence library, and since you’re not supposed to run things like database queries on the main thread (the UI thread), I was looking at other ways to run them.

In general, you probably won’t want to run database queries using a Thread, but just to see how Room works, I wrote this Java Thread code, and confirmed that it works as expected:

How to show an Android Snackbar message alvin August 6, 2017 - 11:29am

To show an Android Snackbar message from an Acitivity or Fragment, use Java code like this:

Snackbar.make(view, "going to: " + url, Snackbar.LENGTH_LONG).show();

One key is to remember to call the show() method after make(). I have a tendency to forget to call show() and then wonder why my Snackbar message isn’t showing up. So maybe a better way to show that code is like this:

8 ways to do asynchronous processing in Android and counting alvin August 3, 2017 - 5:27pm

This is a good article that shows several different ways to handle background tasks in Android, including

  • AsyncTask
  • IntentService
  • Loader
  • JobScheduler
  • CountdownTimer
  • Java threads
  • Android HandlerThread
  • FutureTask
  • Java Timer
When is the Android Fragment onCreateOptionsMenu method called? alvin August 3, 2017 - 12:36pm

Android FAQ: When is the Android Fragment onCreateOptionsMenu method called?

I was just working through a problem with an Android Menu and MenuItem, and added some debug code to the methods in my Android Fragment, and found that the onCreateOptionsMenu method is called after onStart. I didn’t put Log/debug code in every activity lifecycle method, but for the ones I did add logging code to, the specific order of the fragment method calls looked like this:

Android: How to convert a list of strings or objects to a single string alvin August 2, 2017 - 4:39pm

Android FAQ: How do I convert a list of strings (or a list of objects) to a single, combined string?

In Android, if you want to convert a list of strings to a String, I just saw this approach:

How to use the Android 7 split screen feature (tutorial) alvin July 25, 2017 - 10:23pm

Android 7 comes with a cool new “split screen” feature where you can look at two apps running at the same time. This little pictorial/tutorial shows how to use this split-screen feature.

My Android AsyncTask docs and examples (parameters, callbacks, executing, canceling)

I’ve currently written this document as a “note to self” about how the Android AsyncTask works. It’s currently incomplete, but if you want to know how an AsyncTask works, most of the answers are generally here. I provide documentation for most aspects of the AsyncTask, though my coverage of (a) updating progress/status and (b) canceling an AsyncTask is a little weak atm.

Source code for an Android AsyncTask (REST client) example alvin July 20, 2017 - 12:41pm

I just got back into using an Android AsyncTask, and it took me a little while to re-load the concepts in my head. I used AsyncTask’s a few years ago, but haven’t used them since.

To help remember how they work, I created a little AsyncTask example project, and I’ve included all of the source code for that project here. I’ll show all of the source code for my classes and configuration files, and then explain the code at the end.

Android Room database persistence library examples alvin July 20, 2017 - 12:02pm

As a note to self, here are some Android Room database persistence library examples:

Those tutorials don’t show how to properly use Room database access methods, so they’ll lead to Android “Application Not Responding” (ANR) errors. Therefore, here are some related Google/Android docs:

Finally, here’s my own Android AsyncTask REST example, which also shows how to use an AsyncTask.