-2

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();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
UnknownBoy
  • 169
  • 7

2 Answers2

1

The best way is probably to use Intent. Example code to send data in first activity (it will automaticly go to next activity):

//change MainActivity.this (active class) and SecondActivity.class (second class) to your classes
Intent shareData = new Intent(MainActivity.this, SecondActivity.class);
shareData.putExtra("keyName","dataToSend");
startActivity(shareData);

Code to receive data (Second activity):

    final Intent intent = getIntent();
    if (intent.getExtras() == null) {
        // Do first time stuff here
    } else {
        // Do stuff with intent data here
        Bundle b = getIntent().getExtras();
        recivedData = b.getString("keyName");
    }
-1

Use interfaces :

First, create an interface like this.

public interface Communicator {

    void sendData(String data);

}

then have your first activity implement your interface and override the method.

then in your third activity initiate your communicator like this.

    Communicator comm =  new FirstActivity();

now whenever you want to communicate with your first activity. just use the comm.sendData() method and you will receive it in the override method

Kmelliti
  • 101
  • 7
  • In Android, we are not instantiating Activities. – Dhaval Jul 27 '19 at 15:38
  • You won't be sending the data to the first activity instance, why? because you are creating a new instance of the activity that is already present in the memory. – Taseer Jul 27 '19 at 15:39