-1

What I want to do is:

  1. When I launch the application, the main activity's interface will be shown but the Global String will have no data stored.
  2. Then when I click the button in the MainActivity, I will be directed to the next Activity.
  3. In this 2nd Activity, there is an EditText where I can input anything I want.
  4. Then if done typing in the editText (lets assume EditText is the same as if-statement condition needed to execute), I will click the button in the 2nd Activity and pass the intent to the MainActivity class.
  5. In the MainActivity, I want to get the intent passed by the 2nd Activity to store it in my String and when I click the button in MainActivity it will show a message that will display a toast saying "Successful".

My problem lies in step 5, when I click the button, it does not show "Successful"; it shows "Error". Assuming that I've typed what I want is the same as what I needed in the if-statement.

My Codes are:

Floating Action Button Codes:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, SearchCategory.class);
                startActivityForResult(i, 0);
            }
        });

Note: This is the button that opens the 2nd Activity

In my 2nd Activity button click codes:

public void search(View view) {

    editText1 = (EditText)findViewById(R.id.start_text);
    type = editText1.getText().toString();



    Intent intent = new Intent();
    Bundle bundle = new Bundle();
    bundle.putString("type", type);
    intent.putExtras(bundle);
    setResult(RESULT_OK, intent);
    finish();

}

The Button Click in Main Activity

public void onButtonClick(View view) {
if (start == "Car") {
            Toast.makeText(this, "Successful", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}

On ActivityResult Codes:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            /*EditText et = (EditText)findViewById(R.id.text_category);*/

            Bundle category = data.getExtras();
            category_text = category.getString("type");

            /*et.setText(start);*/
        }
    }

Note: I've tried adding an editText in main activity, it shows the text that I typed in the 2nd activity's editText.

3 Answers3

1

start is a String value so change this condition to

start.equals("Car")

Rose
  • 176
  • 1
  • 1
  • 10
0

Below code having a problem,

if (start == "Car") {
            Toast.makeText(this, "Successful", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}

You should compare your String with equals("Car")

Plz refere for some basic idea https://www.javatpoint.com/string-comparison-in-java

dharmendra
  • 7,835
  • 5
  • 38
  • 71
0
  1. When your start value changed? It`s never changed in this code. In this case you should do this.

    Bundle category = data.getExtras(); start = category.getString("type");

  2. Or check condition by category_text like below

    if (category_text.equals("Car")) { Toast.makeText(this, "Successful", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Error", Toast.LENGTH_LONG).show(); }

Fr099y
  • 734
  • 7
  • 15