i would like to have a "global" object that persist in the application, even when you start other activity or fragment, its kind of annoying to use a bundle to pass the same elements in the app. Maybe with viewmodels? i dont know too much about that , so it would be handy if you give me an example or some guidance in this subject. Thank you in advance:)
Question posted in Android Studio
The official documentation can be found here.
The official documentation can be found here.
2
Answers
I ended using something similar to an static var, but there is no static var in kotlin, so i used companion objects, it can store a variable perfectly :)
and its called from any activity or fragment as follows: ApplicationData.string = "Hi, im a string"
Basically it’s pretty simple.
Step 1: Create a java class which extends the Application.
Step 2: Define the Application name as your class.
Here add android:name=".MyApplication" inside <application/> tag.
Step 3: Now use this where you want inside your application.
That’s it 🙂