After pressing the button I would like to go to the second activity, then from the second activity to the third activity. In the third activity after entering the data and pressing the button, I would like to go back to the first activity with the displayed data, which I introduced in the third activity. Is this the way that I present below is correct? Because I have tried many other ways and only this concept brings positive results. But I would like to make sure that this is the right way? This is my code:
First Activity:
public class MainActivity extends AppCompatActivity {
TextView textViewInformation;
Button button_GoToSecond;
String name;
public static final int REQUESTCODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInformation = findViewById(R.id.textView);
button_GoToSecond = findViewById(R.id.button);
button_GoToSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Second.class);
startActivityForResult(i, REQUESTCODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent i) {
if((requestCode == REQUESTCODE) &&(resultCode == RESULT_OK)) {
name = i.getStringExtra("name");
textViewInformation.setText(name);
}
}
}
Second Activity:
public class Second extends AppCompatActivity {
Button button_GoToThird;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button_GoToThird = findViewById(R.id.button2);
button_GoToThird.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Second.this, Third.class);
startActivityForResult(i, MainActivity.REQUESTCODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent i) {
if((requestCode == MainActivity.REQUESTCODE) &&(resultCode == RESULT_OK)) {
String name = i.getStringExtra("name");
Intent j = new Intent();
j.putExtra("name", name);
setResult(RESULT_OK, i);
finish();
}
}
}
And Third Activity:
public class Third extends AppCompatActivity {
EditText editText_Data;
Button button_SendData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
editText_Data = findViewById(R.id.editText);
button_SendData = findViewById(R.id.button3);
button_SendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
test();
}
});
}
public void test() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent();
i.putExtra("name", name);
setResult(RESULT_OK, i);
finish();
}
}