I have 2 activities: MainActivity and createRecipe, and a class Recipe. MainActivity directs me to createRecipe where I can create a new recipe. My problem is after creating the recipe, I want to save that in an ArrayList recipeList which was created in MainActivity. How would I access that array from createRecipe to save it?
Here's my code for createRecipe activity
// Create EditText objects from the layout
EditText etRecipeName = findViewById(R.id.recipeName);
EditText etDuration = findViewById(R.id.duration);
EditText etIngredient = findViewById(R.id.ingredients);
EditText etDesc = findViewById(R.id.description);
EditText etSteps = findViewById(R.id.steps);
// onClickListener for createRecipe button
Button createButton = findViewById(R.id.createButton);
createButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Checks if there are empty fields
if (isEmpty(etRecipeName, etDuration, etIngredient, etDesc, etSteps) == false) {
Toast.makeText(createRecipe.this, "There are empty fields. Please fill up all fields",
Toast.LENGTH_SHORT).show();
}
// Checks if fields are entered correctly
else {
if (correctType(etRecipeName, etDuration, etIngredient, etDesc, etSteps) == true) {
CreateRecipe(etRecipeName, etDuration, etIngredient, etDesc, etSteps);
Toast.makeText(createRecipe.this, "Recipe Created", Toast.LENGTH_SHORT).show();
// How should I save it
finish();
}
else {
Toast.makeText(createRecipe.this,
"Some fields are entered wrongly. Please try again", Toast.LENGTH_SHORT).show();
}
}
}
});
And in MainActivity, I have
ArrayList<Recipe> recipeList;