I know how to send int, boolean, String and an array of primitive data types from one Activity to another one. What I want to know is how to send an array of CheckBoxes from one activity to another one. Here I am not talking about checkboxes value, I want to trannsfer the CheckBoxes, the GUI part the Sqaureboxes. The array is dynamic, the number of Arrays is specified in the run time, I add the checkboxes array to a LinearLayout. Any ideas?
AddChecklist.java
public class AddChecklist extends AppCompatActivity {
Toolbar toolbar;
Button btnAddItem;
public LinearLayout linearLayout;
ArrayList<String> itemslist;
List<CheckBox> boxList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_checklist);
itemslist = new ArrayList<>();
boxList = new ArrayList<>();
toolbar = findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
btnAddItem = findViewById(R.id.btn_add_item);
linearLayout = findViewById(R.id.linear_layout);
// this handler auto clicks btnAddItem button after the provided time;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
btnAddItem.performClick();
}
},100);
btnAddItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addView();
}
});
}
public void addView(){
View checklistView = getLayoutInflater().inflate(R.layout.checklist_view, null, false);
EditText etChecklistItem = checklistView.findViewById(R.id.et_checklist_item);
CheckBox checkBox = checklistView.findViewById(R.id.check_box);
etChecklistItem.requestFocus();
linearLayout.addView(checklistView);
// this handler makes the keyboard to popup on the Edittext and also
// the editText must requestFocus();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etChecklistItem, InputMethodManager.SHOW_IMPLICIT);
}
},200);
ImageView imgDelete = checklistView.findViewById(R.id.img_delete);
imgDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
removeView(checklistView);
}
});
}
public void removeView(View view){
linearLayout.removeView(view);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.save_delete, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.btn_save:
for (int i = 0; i < linearLayout.getChildCount(); i ) {
View v = linearLayout.getChildAt(i);
CheckBox checkBox = v.findViewById(R.id.check_box);
boxList.add(checkBox);
}
Intent intent = new Intent(this, test.class);
Bundle bundle = new Bundle();
bundle.putSerializable("ch", boxList.toString());
intent.putExtras(bundle);
startActivity(intent);
break;
case R.id.btn_delete:
Toast.makeText(this, "delete button clicked", Toast.LENGTH_SHORT).show();
new Handler(Looper.getMainLooper()).postDelayed(() -> finish(), 500);
break;
}
return true;
}
}
ActivityB.java
public class ActivityB extends AppCompatActivity {
LinearLayout lay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
lay= findViewById(R.id.lay);
Intent intent = getIntent();
Serializable v = intent.getSerializableExtra("ch");
lay.addView((View) v);
}}
My problem here is that I can't reconstruct a dynamically added list of CheckBoxes in ActivityB, because I need the checklist GUI parts to be recreated not just there values.
Appreciate any help.
CodePudding user response:
List<CheckBox> checkBoxes = new ArrayList<>();
Intent intent = new Intent(this, MainActivity2.class);
Bundle bundle = new Bundle();
bundle.putSerializable("checkBoxes", checkBoxes.toString());
intent.putExtras(bundle);
CodePudding user response:
Do not send a View or any of its children across Activities or Fragments.
In Android, a View is tied (for a multitude of reasons) to a Context (which can be an Activity, Fragment, etc.). The link is a hard reference, so if you retain this reference (so you can, for instance pass it along), the previous activity (or fragment) will not be released from memory so as long as you retain this "view".
Instead, store the state you used to construct the checkboxes in your activity1, and pass it along (either via intent, or by relying on a sharedViewModel) to your Activity2, where it will pick the values and reconstruct its own (or even the same) layout.