0

I am trying to pass data between activities

Activity one: Inputs phone number and passes it to the next activity

Activity Two: Gets phone number through intent and sends OTP to that number

First Activity Code:

public class PhoneVerification extends AppCompatActivity {
private EditText edMobileNumber;
private Button btnContinue;

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

    edMobileNumber = findViewById(R.id.edMobileNumber);
    btnContinue = findViewById(R.id.btnContinue);



    btnContinue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String phoneNumber = edMobileNumber.getText().toString();
            Intent intent = new Intent(PhoneVerification.this, OTPVerificationActivity.class);
            intent.putExtra("phone",phoneNumber);
            startActivity(intent);
        }
    });

}}

Second Activity Code :

public class OTPVerificationActivity extends AppCompatActivity {

private TextView mTvResend, mTvTimer;
private ImageView mIvBack;
String verificationCodeSentToUser;

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

    
    mTvResend = findViewById(R.id.tvResend);
    mTvTimer = findViewById(R.id.tvTimer);
   
    // OTP Process
    String phoneNumber = getIntent().getStringExtra("phone");
    sendVerificationCode(phoneNumber);
    Toast.makeText(this, phoneNumber, Toast.LENGTH_SHORT).show();
    Log.d("Phone", "onCreate: " + phoneNumber);

    // Code Resend
    new CountDownTimer(60000, 1000) { // adjust the milli seconds here

        @SuppressLint("DefaultLocale")
        public void onTick(long millisUntilFinished) {
            mTvTimer.setText(String.format("%d seconds left",
                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
        }

        public void onFinish() {
            mTvTimer.setVisibility(View.GONE);
            mTvResend.setVisibility(View.VISIBLE);
        }
    }.start();}}

Problem is that I didn't get the phone number that I passed from the first activity

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • _sendVerificationCode(phoneNumber);_ is this API call? –  Apr 08 '21 at 08:10
  • Yes, this method sends the OTP . Sending OTP is the next tep I commented the method call and just checked the toast and log .. phone number was not there. – Muhammad Hanzilah Apr 08 '21 at 08:13
  • Comment _sendVerificationCode(phoneNumber);_ this line and check it again –  Apr 08 '21 at 09:51

2 Answers2

0

1st activity use this way to send data

 Intent intent = new Intent(PhoneVerification.this, OTPVerificationActivity.class);
 String value ="111111";
 intent.putExtra("phone", value);

2nd activity where you receive the data

    String result;
    Bundle extras = getIntent().getExtras();
    if (savedInstance != null) {
            result= (String) savedInstance.getSerializable("phone");
    } else {
          if(extras != null) {
                   result= extras.getString("phone");
         }
    }
Subham Naik
  • 411
  • 5
  • 12
0

First check if number is not null then go to the next activity other wise stay in the page you have mistake ia you have to not check in button click event these kind of condition so obviously they didnot get number in second activity....

axar
  • 539
  • 2
  • 17