0

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()
Kelven Lim
  • 73
  • 8

1 Answers1

0

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

M DEV
  • 763
  • 1
  • 7
  • 20
  • will finish() just take me back to the home page? – Kelven Lim Jun 13 '22 at 06:29
  • @KelvenLim yes, it just closes your current activity. But what action do you want to perform with `cancelButton`? – M DEV Jun 13 '22 at 06:31
  • 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 – Kelven Lim Jun 14 '22 at 05:46
  • @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? – M DEV Jun 14 '22 at 05:50
  • 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 – Kelven Lim Jun 14 '22 at 05:53
  • @KelvenLim yeah, finally i understand what you want to say. For this you have to use Start Activity for result. If user click on cancel it receive null value but if user click on recipe button it receive some string values. ryt? – M DEV Jun 14 '22 at 06:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245581/discussion-between-kelven-lim-and-m-dev). – Kelven Lim Jun 14 '22 at 06:05