Android app development tips part 1

We all face some trivial issues during app development which drive us like nuts. Let me help you with few of the tips which will really helpful during the entire life cycle of your app development.

I am assuming, you are an android developer and have basic app development knowledge.

Lets start –

android.app.Application

To maintain the state of app we better need to use this base class. This will have few really helpful methods to play around with the state of your app. These are –

  • onCreate() – This get called when an application get started, so you can use this to initialize any application level object you want to hold.
  • onLowMemory() – This get called when overall system is running low on memory, so that you can destroy any unnecessary created object or to stop any background running services.
  • onTerminate() – This get called when the app is getting terminated, so you can clear and destroy all the application level objects.

Interestingly you can get the object of your inherited Application class by calling like the below code

App app = ((App)context.getApplicationContext());

 UTF-8/UTF-16 Encoding and Decoding

Charset encoding is really life saver if your app needs to talk to your web-services by passing or receiving special characters(like Unicode characters, white spaces etc.). It’s pretty simple to use it.

To encode follow this –

URLEncoder.encode(putYourStringHere, "UTF-8");

Similarly to decode the encoded string –

String s = new String(putYourStringHere, "UTF-8");

 Check internet status before web-service request 

It is really important to check internet status before hitting any web service as this will give better user experience and also save your users battery, here is the snippet for checking the status

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

To check the internet status your need to set permission in AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And you also need to handle runtime permissions in marshmallow running devices

 

This is just the beginning of this series and I will publish further articles about app development tips.

Till then

Happy coding!!!

Leave a Reply

Your email address will not be published. Required fields are marked *