So I am trying extend the base Application
class and add member variables to create global variables like in this first solution of the link below.
Android global variable
This works if the member variable is a simple data type like a String
or a Boolean
. But how would you do it for a more complex data type? In my case i would like the member variable to be of type HashMap<String, Boolean>
.
I am setting three member variables in onActivityResult()
(a boolean, a String
, and a HashMap<String, Boolean>
), and i am trying to access these member variables in the onClick()
method of a button's onClickListener
. When i access the string and boolean variables their values are set appropriately. However when i access the HashMap<String, Boolean>
variable its value is set to '{}', an empty HashMap
. Is there some kind of Serialization
that needs to happen with a complex data type?
I have also added an ArrayList<Boolean>
as a member variable and when i accessed this variable in the onclick()
method it was set correctly. Possibly HashMaps must be set differently. I'm not too sure are this point.
In the code below, I am just showing a stripped down version which only includes the HashMap
member variable.
Here is the my subclass of Application
public class MyApp extends Application {
private HashMap<String, Boolean> selectedContacts = null;
public HashMap<String, Boolean> getSelectedContacts() {
return this.selectedContacts;
}
public void setSelectedContacts(HashMap<String, Boolean> sc) {
this.selectedContacts = sc;
}
}
Below are the methods onActivityResult()
and onClick()
in my Messenger Activity
. In onActivityResult()
, I set the private member variable of the MyApp Class. In the onClick()
method, I call the accessor method.
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case R.integer.contact_manager:
if (resultCode == RESULT_CANCELED)
Log.d(getString(R.string.debug_tag), "FAILURE");
if (resultCode == RESULT_OK) {
Log.d(getString(R.string.debug_tag), "SUCCESS");
MyApp appState = ((MyApp)getApplication());
appState.setSelectedContacts((HashMap<String, Boolean>) data.getSerializableExtra("selectedContacts"));
}
break;
}
}
add_contact_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Messenger.this, ContactManager.class);
MyApp appState = ((MyApp)getApplication());
HashMap<String, Boolean> sc = appState.getSelectedContacts();
if (sc != null) {
int totalContacts = sc.size();
if(totalContacts > 0) {
Bundle bundle = new Bundle();
bundle.putSerializable("selectedContacts",sc);
i.putExtras(bundle);
}
}
startActivityForResult(i, R.integer.contact_manager);
}
});