Konoko_kun
Konoko_kun

Reputation: 17

Firebase's onCompletion bundle doesn't send...?

Here's the problem : My Firebase gets info from a document that WORKS (Log.d and Toast shows that it fetches info correctly)

    public void getUserInfo(String id)
{
    DocumentReference docRef = db.collection("users").document(id);
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                    Toast.makeText(ConnexionActivity.this, "DocumentSnapshot data: " + document.getData(),
                            Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(ConnexionActivity.this,GameMenu.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("USERID", document.getId());
                    bundle.putString("USERNAME", document.get("lastname").toString() + " " + document.get("firstname").toString());
                    bundle.putString("SCORE", document.get("score").toString());
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });
}

However, when creating the GameMenu activity, the bundle (savedInstanceState) is null and trying to get that info gives me exceptions.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_menu);
    ((TextView)findViewById(R.id.usernameView)).setText(savedInstanceState.getString("USERNAME"));
    ((TextView)findViewById(R.id.scoreView)).setText(savedInstanceState.getString("SCORE"));
}

Quick help is very much appreciated, we're kinda in a panic here :(

Upvotes: 0

Views: 52

Answers (1)

1gni5
1gni5

Reputation: 48

It seems that savedInstanceState return null. Not an expert but this works for me:

// Add intent and bundle to your class
private Intent;
private Bundle;
// Initialize them in your onCreate method
intent = getIntent();
bundle = intent.getExtras();
// And get your data using bundle
bundle.getString("KEY")

Hope that solves your problem :)

Upvotes: 1

Related Questions