How can I create global variable keep remain values around the life cycle of the application regardless which activity running..
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Try Like This:
Create a shared data class:
SharedData.java
Declared/Initiaze an instance of class globally in those classes where you want to set/get data (using this code before
onCreate()
method):-Set data:
Get data:
Use SharedPreferences to store and retrieve global variables.
There are a few different ways you can achieve what you are asking for.
1.) Extend the application class and instantiate your controller and model objects there.
Then you can call the your singleton from your custom Application object at any time:
2.) You can have your controller just create a singleton instance of itself:
Then you can just call the create method from any Activity or Fragment and it will create a new controller if one doesn't already exist, otherwise it will return the preexisting controller.
3.) Finally, there is a slick framework created at Square which provides you dependency injection within Android. It is called Dagger. I won't go into how to use it here, but it is very slick if you need that sort of thing.
I hope I gave enough detail in regards to how you can do what you are hoping for.
I checked for similar answer, but those given here don't fit my needs. I find something that, from my point of view, is what you're looking for. The only possible black point is a security matter (or maybe not) since I don't know about security.
I suggest using Interface (no need to use Class with constructor and so...), since you only have to create something like :
Then you can access everywhere within your classes by using the following:
You can create a
Global Class
like this:Then define it in the manifest:
Now you can set values to global variable like this:
You can get those values like this:
Please find complete example from this blog Global Variables