-2

I am trying to pass Bundle from second activity to the first(launch) activity. In order not to get NPE on launch, I am checking if bundle != null, however, it looks, like even after returning from second activity with Bundle, it still doesn't run the "if" body. Here is my part of code of first activity

Bundle bundle = getIntent().getExtras();
    if (bundle!=null) {
            Player player = new Player();
            player.setStatus(bundle.getInt("Status"));
            player.setName(bundle.getString("Name"));
            addPlayerToList(player);
            Log.e("Player with a name " + player.getName(), "Has been   created");
    }

And code of second activity

 submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name = nameEditText.getText().toString();
            if (defaultRadioButton.isChecked()) {
                status=0;
            } else if (driverRadioButton.isChecked()) {
                status=1;
            } else {
                Toast.makeText(getApplicationContext(), "Suka viberi galochku", Toast.LENGTH_SHORT).show();
            }
            Intent i = new Intent(getApplicationContext(),StartActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("Status",status);
            bundle.putString("Name", name);
            Log.d("Object " + name, "Status: " + status);
            startActivity(i);
        }
    });

Thanks for any help/advice

Max Hinkul
  • 13
  • 1
  • In which line does the NPE happen? What variables are used? Can you add a https://stackoverflow.com/help/mcve ? – Robert Sep 08 '16 at 00:08
  • NPE happens in first activity when I am not checking if Bundle exists as I am setting status from bundle which doesn't exist yet. player.setStatus(bundle.getInt("Status")); – Max Hinkul Sep 08 '16 at 00:16

3 Answers3

0

try Intent.putExtra() instead of putting data into a bundle, and use Intent.getStringExtra() to get a String data;

AssIstne
  • 466
  • 4
  • 13
0

In your code, there is no code to put your bundle into intent. actually you never pass the bundle to first activity. you can use this answer to solve your problem.

good luck!

Community
  • 1
  • 1
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
0

Use startActivityForResult() for this situation.

1) You open second activity from the first using this method, not startActivity()
2) Do whatever you want in the second activity
3) Set result bundle
4) Finish activity
5) Open bundle in the first activity

In your case it will look like:

1) Call second activity like this:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, REQUEST_SECOND_ACTIVITY); // request code const

2-4)

submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name = nameEditText.getText().toString();
            if (defaultRadioButton.isChecked()) {
                status=0;
            } else if (driverRadioButton.isChecked()) {
                status=1;
            } else {
                Toast.makeText(getApplicationContext(), "Suka viberi galochku", Toast.LENGTH_SHORT).show();
            }

            final Intent returnIntent = new Intent();
            returnIntent.putExtra("Status", status); // set values
            returnIntent.putExtra("Name", name);
            setResult(Activity.RESULT_OK, returnIntent); // set result code
            finish(); // finish this activity and go back to the previous one
        }
    });

5) Override this method in the first activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch(requestCode) {
        case REQUEST_SECOND_ACTIVITY: // same request code const
            if(resultCode == Activity.RESULT_OK){
                Player player = new Player();
                player.setStatus(data.getIntExtra("Status"));
                player.setName(data.getStringExtra("Name"));
                addPlayerToList(player);
            }
            break;
    }
}
Anton Shkurenko
  • 4,301
  • 5
  • 29
  • 64