ADTT #4 — Test With Low End Devices

If it runs well in slow devices, it’ll (most probably) runs even better in fast devices.

Ahmad Fadli Basyari
AndroidPub

--

Some people test their Android apps in a decent devices. While it’s not wrong, I believe it’s better to also test them in lower device to make sure that everybody, even with low spec device, will have a good experience using our app. Consider this scenario, you have this MainActivity that displays a list of data retrieved from the network.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadDataFromNetwork();
}

You can also go to DetailActivity to, of course, see the detail. And if you go back, the list will still be there. You’re confident about the app and submit it to the Play Store. But then some reviews say that it keeps retrieving the list every time they go back from DetailActivity or when they returned after minimizing the apps. You know it’s not true because you’ve tested it and you can’t reproduce it no matter how you tried.

So what happen? One of the possibility is that your activity is killed by the Android OS when it needs more memory. The OS release memory that are being used by apps in the background by killing the activity.

When the user go back to your activity that is already killed, the app will create a new instance of that activity, calls onCreate() again and reloads all the data again from the network.

This happens quite often in lower end devices with 1GB of memory or less. But with a device with a lot of memory, this scenario is hard to test. Fortunately there’s a way to mimic a low memory condition on high end devices: Don’t Keep Activities.

Go to Settings -> Developer Options -> Check “Don’t Keep Activities”

Note: If you can’t find it, you probably have to unlock it first by go to Settings -> About -> Tap on “Build Number” repeatedly until it says “Developer Options unlocked” or something similar.

With this option enabled, every time your activities are in background, they will be immediately killed by the OS. Now try to open your apps, minimize it and immediately go back to your apps. And indeed, the activity will retrieve the data again from the network.

When the user comes back to the activity that is already killed, they should be not aware of that. They should experience as if the activity never being killed. So, in order to give a good user experience, you can save that variable inside a Bundle that will be transferred from the killed instance of the activity to the new one by overriding onSaveInstanceState(Bundle) method in your activity.

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("data", dataToDisplay);
}

Then in the onCreate method, you can check if there are a savedState from previous instance of the activity.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) loadDataFromNetwork();
else dataToDisplay = savedInstanceState.getParcelable("data");
}

This way, the activity will only load the data from network if it really needs to. You also saved the user bandwidth by doing that. Yay!

Note: Activity is also killed and recreated in other events like configuration changes (device rotation, phone language changes, etc) and this is the common way to save the data.

While we can mimic the memory shortage using Don’t Keep Activities option, it’s still a good idea to test on actual low end device if you can manage to get one. You might found out that fancy animation will struggle to run on low end device among other things related to performance.

Internet Connection

It’s also a good practice to once in a while test your apps in a bad internet connection. Not everyone has access to 3G let alone 4G. A screen that loads fast on your testing environment might takes few seconds to load in 2G connections. So you might consider adding that loading indicator to avoid the impression your apps not responding.

ADTT (Android Development Tips and Tricks) is a 31 series of blog posts that I’m trying to finish in throughout May. Click here for index.

--

--