Global variable extend application class

2019-02-21 05:04发布

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);

  }
});

1条回答
We Are One
2楼-- · 2019-02-21 05:47

So I fixed this issue. In the onActivityResult() method, I was iterating through the HashMap like so:

Iterator<Entry<String, Boolean>> it = sc.entrySet().iterator();
while (it.hasNext()) {
  HashMap.Entry pairs = (HashMap.Entry) it.next();
  String contactId = pairs.getKey().toString();
  boolean selected = (Boolean) pairs.getValue();
  if (selected) {
    selectedContactsText += getName(contactId) + ", ";
    Log.d("Messenger", getName(contactId) + " was selected");
    Log.d("Messenger", selectedContactsText + " is the text");
  }
  it.remove(); // avoids a ConcurrentModificationException
}

However i noticed the last statement of the while loop was in fact removing elements from the HashMap. So I commented this line out and all works as intended.

查看更多
登录 后发表回答