How to go up to parent Activity that need to be started with extras data?

Viewed 83

Suppose I have an app which Activity A will show a post and also its comments. If I click one of the comment, it will go to Activity B. So, Activity B will show comment and also replies for that comment. Up until now, I don't have a problem. I just need to set parent Activity for Activity B to Activity A in manifest.xml.

Now, I also have Activity X which will show user profile and also all of their posts and comments. If I click a comment, it will go to Activity B. But, if I click up button, it will go back to Activity X. How to make it go back to Activity A?

By the way, to start Activity A, I need to supply extra data which contains the ID for that post and for every comments, they also have an ID of their post ID.

Is it possible to do the following in Activity B?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // if come from Activity A:
                NavUtils.navigateUpFromSameTask(this);
            // else
                Intent intent = new Intent(this, ActivityA.class);
                intent.putLong("ID", comment.getPostId());
                startActivity(intent);
            return true;
        default:
    }

    return super.onOptionsItemSelected(item);
}

Or should I use something like TaskStackBuilder to start Activity B from Activity X?

1 Answers
Related