The Java language doesn’t intrinsically support global variables. Every bit of data you store in memory in Java is a member of some class or object. But the Android app framework gives you a way to store application data globally using something called the application context. In this movie, you'll learn how to create your own Application class, how to register it in the app manifest, and then how to store data in memory that can persist for a short amount of time.
- The Java programming language…doesn't intrinsically support global variables.…Every bit of data you store in memory in Java…is a member of some class or object,…but the Android Application Framework…gives you a way to store application data in memory globally…using something called the Application Context.…I'll demonstrate this in the project "GlobalData",…and I'll start by creating a new class…that I'll place in my base package…"com.example.android.java".…
I'll create a new class and I'll name it "GlobalVariables".…I'll get rid of that comment,…and then I'll extend a class named "Application".…This class is a member of the "android.app" package.…I'll click into it and show that it has an interface…called "ActivityLifecycleCallbacks"…and many other tools that you can use…to manage the entire app.…
Now I'll add some data that can be stored by this class.…I'll create a private field,…it'll be a String and it'll be called "textToDisplay",…and then I'll add getters and setters.…In Android Studio you can do this easily…
Watch these Java tutorials to learn smarter, more efficient methods for Android app development.
- Preparing the development environment
- Packaging and running Android apps
- Optimizing Java code for Android
- Implementing event handler interfaces
- Defining custom callback methods
- Working with the Android SDK's Java packages
- Storing data
- Reading text files
- Parsing JSON and XML data
- Managing device sensors
- Playing audio