Solution 1 :
If you want to share 2 data from the first to second activity then use this method
for sharing intent
createButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Checks if there are empty fields
if (isEmpty(etRecipeName, etDuration, etIngredient, etDesc) == false) {
Toast.makeText(MainActivity.this, "There are empty fields. Please fill up all fields",
Toast.LENGTH_SHORT).show();
}
// Create the recipe
else {
createRecipe(etRecipeName, etDuration, etIngredient, etDesc);
Toast.makeText(MainActivity.this, "Recipe Created", Toast.LENGTH_SHORT).show();
Intent changePage = new Intent(MainActivity.this, afterCancel.class); // is this afterCancel.class your recipe activity?
changePage.putExtra("first", firstData); // put here first data
changePage.putExtra("second", secondData); // put here second data or string
startActivity(changePage);
}
}
});
for getting these data in afterCancel.java
String yourFirstData = getIntent().getStringExtra("first");
String yourSecondData = getIntent().getStringExtra("second");
Toast.makeText(this, yourFirstData, Toast.LENGTH_SHORT).show();
Toast.makeText(this, yourSecondData, Toast.LENGTH_SHORT).show();
code for cancel button
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish(); // use this.
}
});
Hope this helps
Problem :
So basically, I have 2 buttons: Cancel, and Create Recipe. Clicking both would take me back to my home page.
“Create Recipe” requires some EditTexts beforehand to be filled before moving to the homepage and “Cancel”
“Cancel” works fine. But “Create Recipe” crashes the emulator
Below are my codes
// createRecipe button onClickListener
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) == false) {
Toast.makeText(MainActivity.this, "There are empty fields. Please fill up all fields",
Toast.LENGTH_SHORT).show();
}
// Create the recipe
else {
createRecipe(etRecipeName, etDuration, etIngredient, etDesc);
Toast.makeText(MainActivity.this, "Recipe Created", Toast.LENGTH_SHORT).show();
Intent changePage = new Intent(MainActivity.this, afterCancel.class);
startActivity(changePage);
}
}
});
// Cancel button onClickListener
Button cancelButton = findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent changePage = new Intent(MainActivity.this, afterCancel.class);
startActivity(changePage);
}
});
For my home page, its just a
this.getintent()
Comments
Comment posted by stackoverflow.com/a/18146745/16765223
see this answer
Comment posted by Kelven Lim
which one explains how 1 page can receive 2 intentions?
Comment posted by M DEV
just add
Comment posted by Kelven Lim
@MDEV i dont know if i did it correctly..but it seems to be worse now. like now both buttons crash the app
Comment posted by M DEV
checkout my answer. And don’t forget to give me feedback
Comment posted by Kelven Lim
will finish() just take me back to the home page?
Comment posted by M DEV
@KelvenLim yes, it just closes your current activity. But what action do you want to perform with
Comment posted by Kelven Lim
so basically, I have a home page (in this case its just called afterCancel) and a page that creates a recipe (in this case it is the main activity). Both cancelButton and createRecipe will bring me back to the home page. The only difference being whether a recipe was created or not
Comment posted by M DEV
@KelvenLim can i know your home page activity name? your cancel button bring back to only Mainactvity or other activity (Please use activity name don’t home page for understanding). So what you want to do with recipe activity?
Comment posted by Kelven Lim
I have a MainActivity page that directs me to this CreateRecipe page. Both the “cancelButton” button and “createRecipe” button will bring me back to the MainActivity page. The only difference being whether a recipe was created or not Im really sorry for any confusion