What I want to do is:
- When I launch the application, the main activity's interface will be shown but the Global String will have no data stored.
- Then when I click the button in the MainActivity, I will be directed to the next Activity.
- In this 2nd Activity, there is an EditText where I can input anything I want.
- 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.
- 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.