简体   繁体   中英

Moving Data from ArrayList in Second activity to main activity - android

I want to move data from an ArrayList in a second activity to my main activity.

This is my main.java file

   super.onActivityResult(requestCode, resultcode, data);


   setContentView(R.layout.activity_main);
   textView = findViewById(R.id.callingOrder);
   ArrayList<String> numbersList = (ArrayList<String>) getIntent().getSerializableExtra("key");
   textView.setText(String.valueOf(numbersList));```

public void onBackPressed(){


Intent intent = new Intent();
intent.putExtra("key", sandwichOrder);
setResult(RESULT_OK, intent);
finish();

the second.java file above ^

I only get a null message on my xml file. Point is to move the data when I press the back arrow button.

In this line, you're looking in the wrong Intent :

 ArrayList<String> numbersList = (ArrayList<String>) getIntent().getSerializableExtra("key");

getIntent() will return the Intent that was used to start your activity. Anything you pass "back" with setResult() will be in the data parameter to onActivityResult() . So write this instead:

ArrayList<String> numbersList = (ArrayList<String>) data.getSerializableExtra("key");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM