define constant to be used by all activities in an application : android

I would use a class holding those values as static and set / get them with static methods.


Simple answer declare that Variable as STATIC FINAL and use that constant in all activity's by the name of you activity.constantname eg:activity1.name

it will take same values whenever you use it, also it will change globally.. takes same values no matters from which Activity you are accessing It.


There are two ways I've used that are effective:

1) Make an interface called Constants or Globals or whatever you want. Define your constant values in that class and make them all public and final. (They have to be public by definition of an interface, but be sure they're final as well). Now simply declare your Activities and any other classes to implement your Constants interface. Now they all have access to the same global values.

2) Make a class Constants instead of an interface. Define all your constants as public static final. Now you can reference all your constant values via Constants.VARIABLE_NAME in any class in your application.

Tags:

Android