简体   繁体   中英

Getter returning null in different class

while making an Android app, I have ran in the following problem:

My MainActivity looks like this:

...
private String token;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
tokenSetter();
}
private void tokenSetter() {
    FirebaseInstanceId.getInstance().getInstanceId()
            .addOnCompleteListener(task -> {
                if (!task.isSuccessful()) {
                    Log.w("TRAZENITOKEN", "getInstanceId failed", task.getException());
                    return;
                }

                // Get new Instance ID token
                String token = Objects.requireNonNull(task.getResult()).getToken();
                setToken(token);

                Log.d("TRAZENITOKEN", "onGetToken: " + token);

                // Log and toast
                // Log.d("TRAZENITOKEN", "onComplete: " + token);
            });
}
public String getToken() {
    return token;
}

public void setToken(String token) {
    this.token = token;
}

I know that the token value is being set as in an another method inside this MainActivity class, when I call getToken(), I get the value.

However, when I try to call getToken from an another Activity, something like this:

...
button.setOnClickListener(view -> {
        FirebaseActions firebaseActions = new FirebaseActions();
        MainActivity  mainActivity = new MainActivity();
        //firebaseActions.setUserNameOnToken(editText.getText().toString());
        if(mainActivity.getToken() != null) editText.setText(mainActivity.getToken());
        else editText.setText("Skafiskafnjak");
    });

(I opted for the editText.setText method for debugging purposes, I am going to use it in the commented way) The code snippet above always goes to the else part as the getToken is null. Why does it return null in this class, if it returns a value in it's own class? Could it be perhaps because I did MainActivity mainActivity = new MainActivity();

An answer would be appreciated. Thanks in advance!

MainActivity mainActivity = new MainActivity();

This activity instance is not the same one that the Android system created where you see the token being set. Besides, we never create an activity with new . The Android system creates activities according to the activity lifecycle and your code must work within this structure. To pass data between activities, you need to send it in the Intent when you call startActivity() . See the documentation for an example of how to do this.

Creating new activity causes new instance of that activity, therefor a new token which would be null

Send the token through the intent as String data from your MainActivity, and then from the second Activity, grab that String data(token), and do with it whatever you want.

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("token", yourTokenValue);
startActivity(intent);

and in onCreate method of your SecondActivity,you can use getIntent().getStringExtra("token"); to retrieve the token value which was passed from the first Activity

String token= getIntent().getStringExtra("token");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM