'Android app - delete item from list following an action in another activity

I'm creating an app where I display a list of pending challenges. When the user clicks on a challenge, he can accept it or ignore it.

Here's what I want to do and I don't know how :

  • if the user accepts or ignore the challenge, call this.finished and remove the challenge from the list
  • if the back button is pressed, do nothing, the challenge is still visible

In short, if the user really responds to the challenge I don't want it to be displayed in the list, but if he doesn't choose any option and press the back button, he didn't choses one of the two actions so I want that challenge to still be visible in the list.

I don't think it's possible to detect what button I've pressed when i go back to my main activity. I've thought about using global variables, but I don't want to misuse them either.

Just to be clear, I'm not asking how deleting a list item. But when to know deleting one depending of the actions of another activity.



Solution 1:[1]

Give your second activity the index you want to remove as a parameter inside the intent and let it finish by returning the index again as an intent extra (by using setresult(Intent i) and then calling finish) inside your first activity catch the result from your second activity by overwriting onActivityResult (http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))

see 3.3. Retrieving result data from a sub-activity in http://www.vogella.com/tutorials/AndroidIntent/article.html for a detailed howTo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jan