Ada
Ada

Reputation: 37

How to pass data between 3 or more activities in Android Studio?

I am trying to create a project that gets your phone number on MainActivity, passes it to the VerifyPhone activity which sends you the authentication code to your phone.

I could only pass the phone number value from MainActivity to VerifyPhone activity but could not get it to a String value to pass it to my THIRD activity.

What I tried to do:

MainActivity information passage:

String complete_phone_num = "+" + phone_num;
            Intent intent = new Intent(MainActivity.this, VerifyPhoneActivity.class);
            intent.putExtra("phonenumber", complete_phone_num);
            startActivity(intent);

VerifyPhone Activity:

    Bundle bundle = getIntent().getExtras();
    String phonenumber = bundle.getString("phonenumber");

then I tried to pass phonenumber to ProfileActivity:

 Intent intent = new Intent(VerifyPhoneActivity.this, ProfileActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        intent.putExtra("phoneNum", phonenumber);
                        startActivity(intent);

Unfortunately it doesn't recognize the variable phonenumber outside of OnCreate.

I am looking to pass the phone number to other activities so I can store it in my database and use it for statements so it will be organized.

Upvotes: 1

Views: 516

Answers (4)

Lucadmin
Lucadmin

Reputation: 157

As @Code-Apprentice said. I also think you need to add a variable outside the Method and set the value of the extra to this variable.

Something like this:

String phonenumber = "";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
phonenumber = bundle.getString("phonenumber");

public void someOtherMethod(){
Intent intent = new Intent(VerifyPhoneActivity.this, ProfileActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    intent.putExtra("phoneNum", phonenumber);
                    startActivity(intent);
}

Another solution would be to pass the String as a parameter to a specific method.

Upvotes: 1

Madonabulia
Madonabulia

Reputation: 71

i believe you are trying to create new intent and start it outside of oncreate methond, but you are declaring the variable phonenumber inside of it, so it cant be reached outside of its scope, try declaring it as a private field of a activity class and initialize it in onCreate() method, then you will be able to use it.

Upvotes: 3

Code-Apprentice
Code-Apprentice

Reputation: 83557

Unfortunately it doesn't recognize the variable phonenumber outside of OnCreate.

You need to declare phonenumber as a private field of the activity. I suggest that you learn about instance fields in Java classes.

Upvotes: 0

Merthan Erdem
Merthan Erdem

Reputation: 6068

intent.putExtra("phoneNum", phonenumber);

Sure that you didn't make a typo and are checking for "phoneNum" in ProfileActivity?

If not what do you mean by outside of OnCreate? are you trying to access it as a member variable? In case you do you should wait for OnCreate in ProfileActivity to finish setting the value first using the method that you already use in VerifyPhone

Upvotes: 0

Related Questions