开发者

Android: How to set static value before onCreate is called

开发者 https://www.devze.com 2023-04-03 00:38 出处:网络
I want to have an \"option\" set before the Activity is created or at least before it starts. If there is a way to do this via the AndroidManifest? Consider this example where we have a global config

I want to have an "option" set before the Activity is created or at least before it starts. If there is a way to do this via the AndroidManifest? Consider this example where we have a global config class that is used in onCreate to instantiate an object (not fully OO for brevity)开发者_开发技巧

public class Global {
    public static boolean visible = false;
}
public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // here is where we want the most up-to-date value of visible
        MyObject obj = new MyObject(Global.visible);
    }
}

Obviously in this case "visible" would be "false". If this were some sort of API library, we would like to provide the option for users to set "visible" to "true".

Update 1

The objective is to have the global class in a pre-compiled library and have its value set by a developer utilizing the library. I am looking for easiest way for the developer to do this when they create their application; I think the manifest is the probably the way to go but I don't know how to inject the value for "visible" via the xml. The answers below using preferences are good but only cover the users point-of-view.

Update 2 IMHO using resources works best here.

<bool name="visible">true</bool>
public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // here is where we want the most up-to-date value of visible
        Resource res = getResource();
        MyObject obj = new MyObject(res.getBoolean(R.bool.visible));
    }
}


I think using SharedPreferences would do what you are looking for, using Global.visible as the default value. Then if the user changes it to true, it will use that value.

boolean makeVisible = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
  "MyVisiblePreference",
  Global.visible);
MyObject obj = new MyObject(makeVisible);

To allow the preference to be updatable without re-compiling or setting (through a Preferences activity), you can load the default preference from resources:

<bool name="MyVisiblePreference">true</bool>

And reference it similarly with:

boolean makeVisible = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
   "MyVisiblePreference",
   getResources().getBoolean(R.bool.MyVisiblePreference));

If the developer does not set the preference to false, it will default to true (based upon the resources value).


For simple objects you can create them like this: (Based off of your code example)

public static boolean visible = false;

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // here is where we want the most up-to-date value of visible
        MyObject obj = new MyObject(Global.visible);
    }
}

For a more complex object you can initialize it with a static initializer like this:

public static boolean visible;

static {
    visible = false;
}

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // here is where we want the most up-to-date value of visible
        MyObject obj = new MyObject(Global.visible);
    }
}


You can subclass android.app.Application, this class has method onCreate that you can override. Your subclass have to be defined in AndroidManifest.xml in <application name="YourApplication">. onCreate of application is called before all other components in your application are created (before any Activity or Service).

0

精彩评论

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