Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8079925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:15:54+00:00 2026-06-05T16:15:54+00:00

So I am trying extend the base Application class and add member variables to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:15:55+00:00Added an answer on June 5, 2026 at 4:15 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extend the SignalR Hub class to create a base class
I'm trying to extend my ActiveRecord class with some dynamic methods. I would like
I'm trying to extend Page class to add some new functionality (ease of use
I'm trying to extend some base classes in Python: class xlist (list): def len(self):
I'm trying to extend a class, then use a method from base class, but
I am trying to extend the mysqli class to make a helper class that
I'm trying to extend a form and add an upload file field from within
I'm trying to extend TextBox control to add watermarking functionality. The example I've found
I'm trying to extend jWysiwyg with an function to add a map from Google
I'm trying to extend the DOMDocument class so as to make XPath selections easier.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.