2

I have made a form in that I have a textView field called"CategoryID",now when the user click on it it will go to a categoryListActivity,In that activity a List with categories are them ,In that List some of categories having subcategories and some not,So from that i want its name and Id ,But my problem is how to send name and id if category may having subcategory?

my basic logic(what i need is):

categoryID(TextView) ->onClick - >goto "CategoryActivity" --> if "category" having subcategory it will go to next activity "subcategory" otherwise it will redirect to the form with "category name" and "ID" if not it will goto "SubCategoryActivity" and from there a list will there the item which i will click will send to form field "categoryName(TextView)".Please help me to solve this.

Jigar jims
  • 157
  • 3
  • 15

1 Answers1

2

You want to use Intent.FLAG_ACTIVITY_FORWARD_RESULT to achieve this. This flag allows an activity to delegate the responsibility for returning the result to another activity.

MainActivity starts CategoryActivity using startActivityForResult(). If CategoryActivity realizes that the user needs to choose a subcategory, then it starts SubcategoryActivity like this:

    Intent intent = new Intent(this, SubcategoryActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
    intent.putExtra("category", category); // Pass the chosen category to the next activity
    startActivity(intent);
    // This activity can finish now. SubcategoryActivity will return the result
    finish();

Notice that SubcategoryActivity is started using startActivity() and NOT startActivityForResult(). SubcategoryActivity should, however, set a result before it calls finish(). That result will be returned to MainActivity in onActivityResult() when SubcategoryActivity calls finish().

David Wasser
  • 93,459
  • 16
  • 209
  • 274