Passing data from second activity to first activity

Max Hinkul

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

Anton Shkurenko

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;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how can we send data from first activity to second activity and second activity to first activity bu using intent

not working with passing the textView text to the UserprofileActivity(second activity) from MainActivity (first activity)

Passing data to activity from widget

Issue: Passing large data to second Activity

I am not able to send data from first activity (MapActivity) to the second activity (NewContact)

Return back to first activity from third and skipping the second activity

How to close/finish the first activity when navigating from first activity to second activity using android navigation component

Passing data from one activity fragment to a different activity fragement

getting error when passing data from one activity to another activity

Moving Data from ArrayList in Second activity to main activity - android

Passing `SharedPreferences` data from Fragment to Activity

Passing data from one activity to another and then printing

Xamarin Android passing data from class to activity

Passing data from listView to another Activity EditText

Passing data from list fragment to new activity

Android - passing data from AsyncTask to Activity

Passing Data from Dialog box to activity

passing data from activity to tabs fragment

Passing data from Activity to Fragment shows nothing

Passing Data from Fragment to Activity using listview

Passing a data from Activity to Tabbed Layout Fragment?

Passing data from intent service to activity

Passing data from activity to swipeable view fragment

Passing Intent data from Activity to Class

passing data from fragment to activity is not same

Passing data from AsyncTask of ViewModel to Calling Activity

Passing data from activity to fragment in android is not working

Passing data from activty B to activity A fragment

Passing data from Activity with ViewPager its to Fragments