In my main Activity I have object with some data in it. I want to delete all this data through PreferenceActivity using method (wipe()) that does this job. How can I do it?
Inside Preferences.java there is a OnPreferenceClickListener which, when activated, expected to use method in object that I need to access.
Thanks in advance.
Just make this object as static.
Then you can access to this object by class name.
Activity.counters.wipe();
But, keeping data in activities is a bad practice in android development
Related
I want to instantiate an object in my MainActivity class that will be used and altered by my other activities.
The object is a custom data type.
Would it best best to declare the object as public static in MainActivity and then just use MainActivity.object throughout my application? Or would it be better to just pass the object around using intents?
The main thing I'm worried about using static is that I've seen people saying you use it when you care about privacy but I'm not too sure what that means so.. hoping to get some input.
Thanks
If you need one object on all app, you can store this object in Application class
Background -
I have an app that currently has a lot of methods in MainActivity to pull and upload data to firebase. It's quite a mess with the other app logic in there..
Problem -
I would like a separate class filled with server (firebase) access methods that can be called from any activity or fragment and run asynchronously. The class also needs access to context and be able to initialise and store variables.
PS. I'm using kotlin if that helps at all
Edit:
Have done more researching to find terms like "utility" and "static" classes which seems like an accurate way to go... If I create a static utility class with Async methods, will that achieve what I'm after? Also, can you initialise and hold variables in a static class?
Thanks :)
Solved
After more research and testing, I created an object that holds my methods and variables and just need to pass context into the relevant methods. Then just simply call objectname.methodname()
I use the putExtra and getSerializable Methods to pass my object to a second activity. It works fine, however, am I required to return this object in order to maintain the changes made in the second activity?
When I run my app, and launch my second activity then call finish() after makimg a change to the object passed to it, if I relaunch that second activity the old object data previous to the change is displayed, does this mean that using the put/get serializable methods are passing a clone of the object, and that in order to keep the changes made on the second activity I must repass the object back to the main activity ?!
I am not sure why would you require such behaviour.However you can try the following methods.
You can make that object as global static variable(preferably in the application class of the app) so that the object is retained between different instances of the activities.
Also if the state of the object is important across app restarts you must plan to write the state of the object in some persistent storage like db/file/shared preference.Refer this link for storing object,
Currently im storing all of my users information and there friends information inside a class i have created called userInfoCore that extends Application so i can store the values in the Context. When my app crashes it gets rid of al those values and my users are forced to relogin, so i would like to store them in SharedPreferences to be grabbed again in the onCreate of my MainActivity.
I know how to store them, thats not the issue. The issue is i dont want to overcrowd my code with repetitive code and put the storing methods in all the onDestroy's of all my Activities, and i cant #Override onDestroy in my userInfoCore class because its not an Activity i imagine?
Some insite would be great. Thank!
EDIT:
Ive found out that this line in the android manifest is causing my Application Context data to be destroyed even when the user presses the home screen. android:launchMode="singleInstance"
My thoughts are YES i could store them in the onPause or do what #CommonsWare suggested. However like i said, i dont want to have to do all of that. If i can find the root of the cause of the issue... which i have. (The singleInstance in manifest) then i would be much happier.
Some insite would be great.
Update your persistent store when the data changes. A custom Application subclass, like any singleton, should only be treated as a cache or other transient spot for data. If you care about the values, persist them, at the point when the data is changed.
This is a good argument for using the Model-View-Presenter (or Model-View-Controller) pattern. By separating your Model (domain data and procedures) from your View and Presenter (Layout and Activity respectively) you only have to write the Store logic once.
Then you have two choices: either do as #CommonsWare suggests, have the model write itself whenever it changes, or add a simple call in each Application's onPause (onDestroy is too late! It may never get called.) to the model to tell it to save itself.
Note: the model can accept a context as a construction parameter for use in finding a shared preferences, or it can create it's own named preferences using the PreferenceManager
I have a a simple Java bean that gets filled in my second activity.
When I return back to my first activity(main activity) I want to have access to this data. But so far I have no idea how to do it.
Can anyone provide me with a code example of how to pass beans between Activities?
Kind regards
Shared Preferences if you have string data
http://developer.android.com/guide/topics/data/data-storage.html#pref
Singleton class if you have objects to pass around.
If you want guaranteed persistance use database.