damonkashu
damonkashu

Reputation: 1823

why isn't the data from my bundle being sent to the new activity?

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.

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.

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?

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

Upvotes: 0

Views: 321

Answers (3)

damonkashu
damonkashu

Reputation: 1823

I fixed the problem, I wasn't closing the activity properly so it would just resume an existing activity and not go through the OnCreate step.

Upvotes: 0

ccheneson
ccheneson

Reputation: 49410

the name string I am passing to the display activity will not be updated when I change it in the menu setting

In the ContactEdit class:

All you are doing is setting the text of the EditText to what you receive from MainScreen (which is "John Doe"). When you click the button mSubmitName, you send the value of mName (which is not even declared in your example).

To send whatever new value you have enter in the EditText, you will have to retrieve it first:

   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();
      intent.putExtra(MainScreen.EXTRA_NAME, mEditName.getText()); // <-- Retrieve the value from the EditText
      setResult(RESULT_OK, intent);
      finish();
    }
  }
}

Upvotes: 0

Pedro Loureiro
Pedro Loureiro

Reputation: 11514

Some times you use "Name" as the key to get the value from the intent and other times you use "NAME". Always use the same and try again.

Thats why you should have a static const string field instead of hardcoding that value everywhere

Upvotes: 1

Related Questions