Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – How do i use multiple intents that go to the same page?

Posted on November 11, 2022 By

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

READ  [FIXED] Change Android CheckBox icon check/unchecked
Powered by Inline Related Posts

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

Android Tags:android-studio, java

Post navigation

Previous Post: [FIXED] android – Overlapping items in RelativeLayout with ImageView and TextView
Next Post: [FIXED] android – Unable to instantiate application com.company.app.MainApplication: java.lang.ClassNotFoundException React Native Expo

Related Posts

[FIXED] android – java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner Android
[FIXED] android – error: non-static method getActivity() cannot be referenced from a static context Android
[FIXED] java – Android Gradle Sync failed: Could not resolve all artifacts for configuration ‘:classpath’ Android
[FIXED] java – Any better alternative instead of SharedPreferences to save/cache much data without online access? Android
[FIXED] android – How to change color of a Like button on react native with galio Android
[FIXED] java – dataSnapshot.getValue(MyClass.class) from Firebase works fine in development but get null values after is posted on Play Store Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme