kaz
kaz

Reputation: 1

application fails when more than one activity calls startActivityForResult

I have 2 activities that call a third activity using startActivityForResult but after the third activity finishes the application fails before onActivityResult is called. When I initially had only 1 activity calling it, it worked fine and if I use the debugger it works, which usually means something is uninitialized. My code is as follows:

Activity A:

button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(ActivityA.this, AddActivity.class);
        startActivityForResult(intent, ADD_CATEGORY);
    }
});

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == ADD_CATEGORY && resultCode == RESULT_OK) {
        if (data.getCharSequenceExtra("Added") != null) {
            String category = data.getCharSequenceExtra("Added").toString();
            categoryList.add(category);
        }
    }
}

Activity B:

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(ActivityB.this, AddActivity.class);
        startActivityForResult(intent, ADD_ITEM);
    }
});

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);       
    if (requestCode == ADD_ITEM && resultCode == RESULT_OK) {
        String item = data.getCharSequenceExtra("Added").toString();
        if (currentList == null) {
            currentList = new ArrayList<String>();
        }
        currentList.add(item);
        Collections.sort(currentList);
    }
}

Activity C:

public class AddActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        final EditText edittext = (EditText) findViewById(R.id.edittext);
        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                  // If the event is a key-down event on the "enter" button
                  if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode ==       KeyEvent.KEYCODE_ENTER)) {
                      // Return added item to the calling activity
                      Intent intent = new Intent();
                      CharSequence text = edittext.getText();
                      intent.putExtra("Added", text);
                      setResult(RESULT_OK, intent);
                      finish();
                      return true;
                  }
                  return false;
            }
        });
    }
}

Am I missing something or could this be an Android bug?

Upvotes: 0

Views: 413

Answers (3)

Chris Noldus
Chris Noldus

Reputation: 2502

While it may be unlikely in this case, its always worth mentioning:

Possibly the most common reason for extras not being set by activities started with startActivityForResult is that they are SingleTask activities.

All activities that are run in their own task will result in onActivityResult() being called with RESULT_OK before the new activity even starts.

Calls to setResult() in SingleTask activities are pointless and will not do anything useful.

Upvotes: 0

Femi
Femi

Reputation: 64700

Try using if(data != null && data.hasExtra("Added")){ inside your onActivityResult functions to verify that there is actually a data variable passed back. If the user hits the back button it will return to the previous Activity but data will be null in onActivityResult.

Upvotes: 1

Vineet Shukla
Vineet Shukla

Reputation: 24031

please make sure your are setting the result in AddActivity as per your activity call...show your logcat..

Upvotes: 0

Related Questions