Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy isn't the data from my bundle being sent to the new activity?
    text
    copied!<p>For reasons I cannot determine, the name string I am passing to the display activity will not be updated when I change it in the menu setting. I have omitted some excess code but hopefully the concepts are there. In particular, when DisplayName is started, I checked with the debugger and the extras put into the intent do not change from their default values (ie: "John Doe"). I note that when I use the options menu activity to change the name, this change is reflected every time I go back to the options menu. However, when I go to the display screen, the name continues to stay the same.</p> <p>I am aware that these values are simple enough that they can be kept in static member variables and I wouldn't normally have to deal with the passing of bundles, however I still am curious as to why my code fails to function correctly.</p> <p>I notice that if I use the menu option to set the name first, then click the display button, the change is reflected. However, after returning to the main screen and changing the name, the display screen continues to keep the same name. This leads me to think that since I press "Back" to return to the main screen from the display activity, then the activity is never killed so it would never change the displayed name. Is this guess correct?</p> <pre><code>public class MainScreen extends activity { private String mName; public static final String EXTRA_NAME = "android.intent.extra.NAME"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mName = "John Doe" Button button = (Button) findViewById(R.id.display_button); button.setOnClickListener(mDisplayButtonListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, SET_CONTACT_ID, 0, R.string.menu_setName); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { switch(item.getItemId()) { case SET_CONTACT_ID: Intent intent = new Intent(MainScreen.this, ContactEdit.class); intent.putExtra(EXTRA_NAME, mName) startActivityForResult(intent, 1); return true; } return super.onMenuItemSelected(featureId, item); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { mName = data.getExtras().getString(EXTRA_NAME); } } private OnClickListener mDisplayButtonListener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainScreen.this, DisplayName.class); intent.putExtra(EXTRA_NAME, mName); startActivity(intent); } }; } public class ContactEdit extends Activity { private EditText mEditName; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mEditName = (EditText) findViewById(R.id.editName); mEditName.setText(this.getIntent().getExtras().getString(MainScreen.EXTRA_NAME)); Button button = (Button) findViewById(R.id.submit_select); button.setOnClickListener(mSubmitName); } private OnClickListener mSubmitName = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); mName = mEditName.getText().toString(); intent.putExtra(MainScreen.EXTRA_NAME, mName); setResult(RESULT_OK, intent); finish(); } } } public class DisplayName extends Activity { private String mName; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mName = getIntent.getExtras().getString(MainScreen.EXTRA_NAME) TextView view = (TextView) findViewById(R.id.name); view.setText(mName); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload