Question

I have a listview with some cetegories which are checkable. I want to start this activity with startActivityForResult and when the users returns to the initial activity this activity will start founding the categories which have been selected and store their ids in a list. The problem is where should i call the onResult method. My first thought is to put it like this:

@Override
protected void onPause() {
    super.onPause();
    if(isFinishing()){
      onResult()....call here       
    }
}

However, as I know the isFinishing() is not the best way to be sure that the activity is finishing and that we can't be sure when it will be called. Is there any better way to return results when the user have finished checking his categories? I mean that I can't send onResult every time the user makes a selection.

Was it helpful?

Solution

You don't have to call onResult() when you want to identify your activity's result, instead use setResult whenever the user makes a selection and when your activity finishes the last set result will propagate back to the activity that started your activity for result.

The code should look like:

onItemSelected(){ // or onClick
     getItem();
     setResult();
}

OTHER TIPS

Call the setResult(int resultCode, intent data) during the selection and override the onActivityResult in your previous activity.

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

  //do something here.

}

I hope this helps you... :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top