-1

I want to pass an ArrayList<String> and an ArrayList<Boolean> to my next activity, but whatever I try, it always seems to overwrite my first ArrayList with my second. Even if I use a boolean array in stead of an ArrayList, the same problem occurs. The KEY's are different and the values as well. I tried many things already (can't remember all of it), including Bundles, but the same keeps happening. What am I doing wrong?

In both cases (and many others), data comes back null.

EDIT: Cleaned up code after a night's sleep, as I noticed there were indeed some mistakes in it. Also, these are 2 ways that I tried to do it, but neither work.

In activity 1:

boolean [] checksDataOut;
private ArrayList<String> data = new ArrayList();

intent.putStringArrayListExtra(EXTRA_TODEVICES, data);
intent.putExtra(EXTRA_TO_DEVICES, checksDataOut);
startActivity(intent);

In Activity 2

public class DevicesInfo extends AppCompatActivity {

public ArrayList<String> data = new ArrayList();
public boolean [] checksDataIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_devices_info);
    Intent intent = getIntent();
    data = intent.getStringArrayListExtra(EXTRA_TODEVICES);
    checksDataIn = intent.getBooleanArrayExtra(EXTRA_TO_DEVICES);
    }
}

In activity 1:

private ArrayList<String> data = new ArrayList();
boolean [] checksDataOut;

Bundle extrasOut = new Bundle();
extrasOut.putStringArrayList(EXTRA_TODEVICES, data);
extrasOut.putBooleanArray(EXTRA_TO_DEVICES, checksDataOut);
intent.putExtras(extrasOut);
startActivity(intent);

In activity 2:

public class DevicesInfo extends AppCompatActivity {

public ArrayList<String> data = new ArrayList();
public ArrayList<Boolean> checksData = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_devices_info);
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    data = bundle.getStringArrayList(EXTRA_TODEVICES);
    checksData = (ArrayList<Boolean>) bundle.get(EXTRA_TO_DEVICES);
    }
}
Anzupe
  • 11
  • 2
  • 1
    In the code you are adding the variable `checksDataOut ` to the bundle, but the variable you declared is `checksData `. What is `checksDataOut `? – jonathanrz Apr 21 '18 at 23:28
  • Update your question with code with which you get the intent object while readung tge values. – Sagar Apr 21 '18 at 23:46
  • `checksDataOut` is an array of booleans (see first line of code). In the rest of the code, data will be filled into it. – Anzupe Apr 21 '18 at 23:47
  • `getBooleanArrayExtra` reads an Array, not a List – OneCricketeer Apr 22 '18 at 00:48

2 Answers2

0

Personally, I prefer placing that variable in the main activity, the activity which will only die when I close the program. I put it there as a public static variable. So when I launch a new activity, I just instruct it to retrieve data from the variable onCreate or onResume. I know some may say, it's poor programming. At least I can control the content effectively.

In base Activity: or Activity1 create these static fields:

public static ArrayList<String> data;
public static ArrayList<Boolean> checksData;

just before you call Activity2, update these 2 variables.

then call the Activity2 normally.

as Activity2 is created or resumed, where you need data, call data in Activity1, like this Activity1.data; where you want to use checksData, call Activity1.checksData.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Francis Nduba Numbi
  • 2,499
  • 1
  • 11
  • 22
0

First, If you plan on using getBooleanArray, you need to pass a boolean[] using putExtra not a list.

Second, it's null if it cannot be found. It's not clear where your constants are defined but they must be the same, so try using a regular string while debugging.

Putting

boolean[] checksDataOut;
...
Bundle extrasOut = new Bundle();
extrasOut.putBooleanArray("boolArray", checksDataOut);
intent.putExtras(extrasOut);
// or intent.putExtra("boolArray", checksDataOut);
startActivity(intent);

Getting

public boolean[] checksDataIn;
...
Intent intent = getIntent();
checksDataIn = intent.getBooleanArrayExtra("boolArray");

If it's still null, put a break point or log the entire contents of the Bundle to the logcat.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • When I tried debugging, I found out that it's possible for me to send either the String Array List or the boolean Array, but I can't send both together. – Anzupe Apr 22 '18 at 09:53
  • I would recommend not having your two constants be so closely named. You may have a typo – OneCricketeer Apr 22 '18 at 09:56