开发者

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

I want to define constants which can be used by all activities in an application. What is the be开发者_如何学Cst way to do it.

Is extends Application only way of doing it, since I dont want to declare same constant in all the classes.


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.


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.


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


If you want to assign all Activities to a Constant you declared by Creating Constant Class then, Enter this Lines to your Constants class:

public static Class_Name Constant_name = null;

and in the Activities, add this line into onCreate() method:

Class_Name Constant_name = this;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜