Android Studio中如何创建全局类实现跨类属性同步更新?
Absolutely! Using an Application subclass like you’ve started is actually one of the standard, reliable ways to maintain a synchronized global state across all classes in your Android app. Let me break down how this works and how to refine your approach:
Why your current setup works (and how to make it better)
Your CheckForAlreadyLoggedIn class extends Application, which is a singleton instance created once when your app launches and persists for the entire app lifecycle. Every class in your app that accesses this instance will be interacting with the same underlying state—so any changes via setState() will immediately be reflected when calling getState() from any other class.
A small tweak to your code will make it more aligned with Android best practices (no need for static since the Application itself is a global singleton):
public class CheckForAlreadyLoggedIn extends Application { private int state; @Override public void onCreate() { super.onCreate(); // Initialize your state with a default value here state = 0; } public int getState() { return state; } public void setState(int s) { // Fixed method name to follow Java camelCase state = s; } }
How to access this global instance in other classes
To use this in an Activity, Fragment, or any other context-aware class:
// In an Activity CheckForAlreadyLoggedIn app = (CheckForAlreadyLoggedIn) getApplicationContext(); // Update the state app.setState(1); // Get the current synchronized state int currentState = app.getState();
For non-context classes (like utility classes), you’ll need to pass a Context to get access to the application instance:
public class MyUtilityClass { public void updateAppState(Context context, int newState) { CheckForAlreadyLoggedIn app = (CheckForAlreadyLoggedIn) context.getApplicationContext(); app.setState(newState); } }
Critical setup step
Don’t forget to register your custom Application subclass in your AndroidManifest.xml—otherwise, Android will use the default Application instance instead:
<application android:name=".CheckForAlreadyLoggedIn" <!-- Other application attributes --> > <!-- Activities, services, etc. --> </application>
Bonus: Thread safety (if needed)
If your state might be modified or read from multiple threads (e.g., background workers), add thread safety to avoid race conditions. You can either use synchronized methods or an AtomicInteger:
// Option 1: Synchronized methods public synchronized int getState() { return state; } public synchronized void setState(int s) { state = s; } // Option 2: AtomicInteger (simpler for primitive ints) private AtomicInteger state = new AtomicInteger(0); public int getState() { return state.get(); } public void setState(int s) { state.set(s); }
Final note
This approach is perfect for simple global state like your login status. It ensures that every class in your app is working with the exact same state value—any update is instantly visible everywhere you call getState().
内容的提问来源于stack exchange,提问作者Muhammad Nauman

