Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static fields in Android

I am newbie in android and java development and i want to understand some things.

  1. Is it bad practice to use static field for transfer data between activities and fragments? For me, it is much easier than using extras in intents.

  2. Why many programs use context as parameter in constructors and methods when we can use static field to one point access for application context? For example:

    public class ApplicationLoader extends Application {
    public static volatile Context context;
        @Override
        public void onCreate() {
            super.onCreate();
            context = getApplicationContext();
        }
    }
    

So can someone explain, in which cases should use static fields in android (i mean for classes such as adapters, lists, context, etc)? And when using non-static fields, getters/setters, extra in intents is better way then static fields?

like image 385
hluhovskyi Avatar asked May 05 '15 16:05

hluhovskyi


1 Answers

Is it bad practice to use static field for transfer data between activities and fragments?

Generally speaking, yes. There are certainly Android patterns for using static data members, but for "transfer data", they are rarely used, for all the classic reasons why global variables are considered to be poor form in most programming languages.

The primary exception is when the activities are in the same app and the data to be passed is either too big to go in an extra or is of a data type that cannot go in an extra. Even then, you are better off with a more flexible approach (e.g., have a static singleton manager of some object cache, where you have the shared data, where you use extras for passing IDs or keys around for accessing the cache).

For me, it is much easier than using extras in intents.

You do not use extras to communicate from an activity to a fragment, or vice versa. At most, you use extras as part of passing data between activities.

Beyond that, too many inexperienced developers value today's pain over "the sum of all tomorrow's pain". In other words, the use of global variables has been demonstrated time and again to reduce maintainability of a program over time. You may not care right now. Your co-workers care. Your boss cares. The future you will care, when you try to maintain the code that you are writing today.

when we can use static field to one point access for application context?

Among other reasons, frequently that is the wrong Context to use. Only use the Application instance when you know precisely why using the Application instance is the right Context for the circumstance. For example, creating UIs from Application (e.g., via LayoutInflater) frequently gives you the wrong results.

This epic blog post by Dave Smith helps outline the roles of the different types of Context.

in which cases should use static fields in android

Avoid them where possible. Use them sparingly where needed. Bear in mind that your process can be terminated at any point when your app is not in the foreground, and so static data members need to be a cache for data stored in a persistent fashion, if you don't want to lose that data.

i mean for classes such as adapters, lists, context, etc

Assuming that by "lists" you mean ListView, using static data members is inappropriate for any of those cases, IMHO.

like image 75
CommonsWare Avatar answered Sep 21 '22 21:09

CommonsWare