0

I'm developing a project where I have 3 activities. Each activity has got it's own button which when clicked starts the next activity. 1st activity has got a Text View which displays Random String. When I press the button in 1st activity, 2nd activity will start. When i press the button in 2nd activity, 3rd activity is started. But I want to know how to send the random string from 1st activity to 3rd activity.

First Activity:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class SelectRandomNumber extends AppCompatActivity {


    private Button generateStringBtn;
    private TextView randomOne;
    private TextView randomTwo;
    private TextView randomThree;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_random_number);

        generateStringBtn = (Button) findViewById(R.id.generateRandomBtn);
        randomOne = (TextView) findViewById(R.id.randomStringOne);
        randomTwo = (TextView) findViewById(R.id.randomStringTwo);
        randomThree = (TextView) findViewById(R.id.randomStringThree);

        generateStringBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                randomOne.setText(randomString(173));
                randomTwo.setText(randomString(173));
                randomThree.setText(randomString(173));

            }
        });

        randomOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendID_one();
            }
        });



    }

    public String randomString(int length){
        char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for(int i = 0; i < length; i++){
            char c = chars[random.nextInt(chars.length)];
            stringBuilder.append(c);
        }
        return stringBuilder.toString();
    }

    public void sendID_one(){
        String message = randomOne.getText().toString();
        Intent check = new Intent(SelectRandomNumber.this, CheckCandidateID.class);
        check.putExtra("Extra_Message",message);
        startActivity(check);

    }
    public void sendID_Two(){
        String message = randomTwo.getText().toString();
        Intent check = new Intent(SelectRandomNumber.this, CheckCandidateID.class);
        check.putExtra("Extra_Message",message);
        startActivity(check);

    }
    public void sendID_Three(){
        String message = randomThree.getText().toString();
        Intent check = new Intent(SelectRandomNumber.this, CheckCandidateID.class);
        check.putExtra("Extra_Message",message);
        startActivity(check);
    }

    public void send(){
        Intent check = new Intent(SelectRandomNumber.this, Try.class);  //for sending data to third activity
        check.putExtra("Extra_Message_Send",randomOne.getText().toString());
        startActivity(check);
    }

}

Third Activity:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Try extends AppCompatActivity {

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_try);

        tv = (TextView) findViewById(R.id.test);

        String s = getIntent().getStringExtra("Extra_Message_Send");
        tv.setText(s);



    }
}
aashik
  • 29
  • 8

2 Answers2

1

Use intent to pass data between the activities. In your first activity,

Intent mIntent = new Intent(FirstActivity.this, ThirdActivity.class);
mIntent.putExtra("randomString", tv.getText().toString());

In the third activity,

String s= getIntent().getStringExtra("randomString");
Faisal
  • 86
  • 1
  • 8
  • It is worth noting that it is common pattern to define a `public static Intent createIntent(Context context, String randomString) {` method in `ThirdActivity` so that you don't need to expose `"randomString"` as a hard-coded string across multiple files. – EpicPandaForce Mar 30 '19 at 16:01
  • Not working! I have updated my code. can you please go through it once? – aashik Mar 30 '19 at 17:23
  • The problem is in the method send(), what you're doing there is passing data from first activity to the third activity and then starting the third activity, which should actually be started from second activity. To pass the intent an inappropriate way here would be FirstActivity (send the edittext value)->SecondActivity (put send() here)->ThirdActivity You can use a singleton class to persist the data. – Faisal Mar 30 '19 at 18:01
1

If you want to start 3rd activity from 2nd activity, you have to send your string in 2nd activity then 3rd activity. Better send all three string in 2nd and 3rd activity, so you can come back and forth:

String message1 = randomOne.getText().toString();
String message2 = randomTwo.getText().toString();
String message3 = randomThree.getText().toString();

check.putExtra("Extra_Message1",message1);
check.putExtra("Extra_Message2",message2);
check.putExtra("Extra_Message3",message3);

Then you have to pass these data from 2nd to 3rd activity also.

ashraf
  • 54
  • 1
  • 7