0

I have a singleton class that has 2 Hashmaps, both private with their own individual(tested and working) getters and setters

private static Property property = new Property();

public static Property getInstance(){
    return property;
}

private HashMap<String, Object> roomReturnedObj = new HashMap<>();
private HashMap<String, Object> roomTasks = new HashMap<>();

public void setRoomObjectFromResult(HashMap<String, Object> roomHashMap) {
    roomReturnedObj.putAll(roomHashMap);
}

public HashMap<String, Object>getRoomObjectFromResult(){
    return roomReturnedObj;
}


public HashMap<String, Object>getRoomTasks(){
    return roomTasks;
}

public void setRoomTasks(HashMap<String, Object> tasksHashMap){
    roomTasks.putAll(tasksHashMap);
}

The lifecycle of the app gathers tasks in the roomTasks HashMap, once gathered a parent name is assigned to this HashMap, the parent name becomes a new roomObject HashMap key with the roomTasks HashMap becoming it's object.

It all behaves strangely though when I try to begin a new run through of room tasks gathering, before I begin I clear with:

Property.getInstance().getRoomTasks().clear();

That function above clears the roomReturnedObj HashMap?

Setting class

saveBtn.setOnClickListener(View ->{
            Bundle resultFinal = new Bundle();
            String roomName = Property.getInstance().getRoomCount()+ ": " + Property.getInstance().getRoomName();

            HashMap<String, Object> room = new HashMap<>();
            room.put(roomName, Property.getInstance().getRoomTasks());

            Property.getInstance().setRoomObjectFromResult(room);

            Property.getInstance().setRoomCount(Property.getInstance().getRoomCount());

            Log.d("Test", Property.getInstance().getRoomObjectFromResult()+ " : " + Property.getInstance().getRoomTasks());

            Intent returnedResultIntent = new Intent(getBaseContext(), NewJobStart.class);
            returnedResultIntent.putExtra("addRoomResult", "Success");
            startActivity(returnedResultIntent, resultFinal);
        });

Class that clears

if (bundle != null) {
            tradeTitles = bundle.getStringArrayList("TradeTitleArray");
            Log.d("Test", "NewJobStart, tradeTitles received from bundle" + tradeTitles + ", Room count: " + Property.getInstance().getRoomCount());

            if(Property.getInstance().getRoomCount() > 0) {
                String test = bundle.getString("addRoomResult");
                Log.d("Test", "NewJobStart, result from jobRoomDetails received from bundle" + test);

                if (test.equals("Success")) {
                    Log.d("Test", "NewJobStart - return, before clear: " + Property.getInstance().getRoomObjectFromResult());
                    Property.getInstance().getRoomTasks().clear();
                    putToList();
                }
            }
        } 
Jay.Smyth
  • 77
  • 1
  • 10
  • You sure your singleton isn't being reinstantiated due to app lifecycle? Yoi sure you're running the code you think you are? The problem is unlikely to be in this code specifically. – Dave Newton Apr 01 '21 at 00:43
  • @DaveNewton I added an edit to show the setting and clearing classes to hopefully shed light on why I'm confused – Jay.Smyth Apr 01 '21 at 00:45

1 Answers1

0

The issue here is that Java is a pass by reference on Objects, even though it looks like a new HashMap and value are created, at the time of passing Objects only a reference is called. Simple fix in my onClick

saveBtn.setOnClickListener(View ->{
            Bundle resultFinal = new Bundle();
            String roomName = Property.getInstance().getRoomCount()+ ": " + Property.getInstance().getRoomName();
            //Create a new roomTasks Hashmap locally here!!
            HashMap<String, Object> roomTasks = new HashMap<>();
            roomTasks.putAll(Property.getInstance().getRoomTasks());
            //Then pass it back to the singleton class
            HashMap<String, Object> room = new HashMap<>();
            room.put(roomName, roomTasks);

            Property.getInstance().setRoomObjectFromResult(room);

            Property.getInstance().setRoomCount(Property.getInstance().getRoomCount());

            Log.d("Test", Property.getInstance().getRoomObjectFromResult()+ " : " + Property.getInstance().getRoomTasks());

            Intent returnedResultIntent = new Intent(getBaseContext(), NewJobStart.class);
            returnedResultIntent.putExtra("addRoomResult", "Success");
            startActivity(returnedResultIntent, resultFinal);
        });
Jay.Smyth
  • 77
  • 1
  • 10