Reputation: 266
In my Android project I have an Activity and a Widget (Broadcast Receiver). Both have a simple button. When either of the buttons is clicked, the same action should be executed: First of all, a Boolean variable should be set to it's opposite value (true to false, false to true). This Boolean should be stored independently from both the Activity's and the Receiver's life cycles. Then, depending on the Boolean's old state, one of two methods should be executed to query a SQLite Database.
I need something like a Singleton Activity, which has its own Shared Preferences and methods and can be called from various Activities / Receivers.
What would be the best way to implement something like this?
Upvotes: 0
Views: 197
Reputation: 243
Doesn't sound like anything you want to do requires an activity. I would just write a POJO singleton with a private constructor and a static getInstance() method that returns a reference to a private static instance of the class itself, after initializing it if needed.
Then you can have an instance method that toggles the variable and fires off an AsycTask or whatever to do your query. The state of the boolean can be stored in a private instance variable, or persisted to SharedPreferences depending on how long it needs to be retained.
Upvotes: 1