I have two classes Class1=Manage and Class2=Scan. I want to get the list from manage and check it with a method in Scan. Here is the code.
The result that i get is "no" and with debugging i saw that list is empty but i don't know why.

Manage.class

  private  ArrayList<String> soy2= new ArrayList<String>();

  ....
   public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

        //12-1-2019
        if (soy.isChecked()) {
            soy1="yes";

           //checked();
            soy2.add("Soy");
            soy2.add("Σόγια");
            soy2.add("soja");
            soy2.add("Soybeans");
            soy2.add("soybeans");
            soy2.add("en:soybeans");
            System.out.println(soy2);
//            //textView.setText("Soy");
//            textView.setText("Soy");
//            System.out.println(soy2);
//            System.out.println(checkedAllergens);
//
        }
        else {
            soy1="no";
            //soy2.remove("Soy");
            soy2.remove("Soy");
            soy2.remove("Σόγια");
            soy2.remove("soja");
            soy2.remove("Soybeans");
            soy2.remove("soybeans");
            soy2.remove("en:soybeans");
            System.out.println(soy2);
        }

   }

            //set value
    public void setArrList (ArrayList soy2){

        this.soy2=soy2;
    }
        public ArrayList<String> getList() {
        System.out.println(soy2);
            return this.soy2;
        }

Scan.class

  private Manage checkd = new Manage();
  ...
  List<String> fetchedAllergensList = new ArrayList<String>();
  .....

  //test() method is calling on a click listener
  //i get fetchedAllergensList from another click listener
   public void test() {

      ArrayList<String> list = checkd.getList();
     // System.out.println(list);
        ElementExist(list,fetchedAllergensList);

    }
    public boolean ElementExist(ArrayList<String> list1, List<String> list2) {
        for (int i= 0; i<list2.size(); i++) {
            if (list1.contains(list2.get(i))) {
                // element list2[i] exist in list1
                testtxt.setText("yes");
                return true;

            }
        }
        testtxt.setText("no");
        return false;
    }

Dani AI

Generated

Most likely cause: Scan is reading a different Manage instance (or reading before Manage’s checkbox handler has run). In the posted thread shows list population happening inside the checkbox callback, while Scan constructs/uses its own Manage object and calls the getter — that new object has an empty list. ’s point about the sequence is also relevant: the getter must be called after the list is populated.

Concrete, practical fixes (ranked by best-practice):

  • Pass the list when launching Scan (recommended for Activity-to-Activity). Example:

    // when starting Scan from Manage
    Intent i = new Intent(this, Scan.class);
    i.putStringArrayListExtra("soyList", soy2);
    startActivity(i);
    
    // inside Scan.onCreate()
    ArrayList<String> soyList = getIntent().getStringArrayListExtra("soyList");
    if (soyList == null) soyList = new ArrayList<>();
  • Use a result-to-caller flow if Manage should return the current selection (startActivityForResult or the newer Activity Result API), so the caller gets the updated list rather than trying to instantiate Manage.

  • Share data via a proper shared store for process-wide state: a small singleton repository or an Application-scoped holder, or (if using fragments) a shared ViewModel. This avoids Activity-constructor misuse and keeps lifecycle explicit.

What to avoid and troubleshooting checklist:

  • Do not instantiate Activities with new — that creates an independent object with no UI lifecycle and empty state.
  • Avoid static globals as the first choice; they work quickly but can cause lifecycle/memory issues and make bugs harder to reproduce (as suggested).
  • Add logging right after the checkbox handler and just before Scan reads the list to confirm the order and contents (Log.d with sizes and a sample element).
  • Confirm the code path that populates the list actually runs before Scan reads it.

Applying one of the recommended patterns will ensure Scan reads the same populated list rather than a fresh, empty instance.

Recommended Answers

All 2 Replies

I can't see any obvious errors in that code, but to go any further we would need to understand the sequence of events that make up your test case.

Make your list static and then populate it in Mange class. Now you will be able to access this List in your scan class.

commented: Just make everything static public, none of that OO nonsense -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.