Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startActivityForResult usage for multiple activities

Tags:

android

i use startActivityForResult for two activities like this

Activity A -> startActivityForResult(ActivityB,0) 
 -> Activity B -> setresult(10); finish();
 -> Activity A -> if(result==10) {
    dofunction();
 }

but i must use this logic for 4 or more activities.

 Activity A -> startActivityForResult(ActivityB,0) 
 -> Activity B -> finish();
 -> Activity C -> finish(); 
 -> Activity D -> setresult(10); finish();
 -> Activity A -> if(result==10) {
   dofunction(); 
}

but this does not work.

like image 979
sipdorus Avatar asked Oct 09 '14 13:10

sipdorus


2 Answers

SOLUTION:

Activity A -> startActivityForResult(activityB,0);
Activity B -> activityCintent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); startActivity(activityCintent); finish();
Activity C -> activityDintent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); startActivity(activityDintent); finish();
Activity D -> setresult(10); finish();
Activity A -> if(result==10) {dofunction(); } YEPPP

this is the logic of FLAG_ACTIVITY_FORWARD_RESULT. First you start an activity for result, new activity sends this intent to another new activity until you set result. I hope this will be helpful for people having same problem.

like image 52
sipdorus Avatar answered Sep 28 '22 03:09

sipdorus


After naming activity in startActivityForResult(ActivityB,0) we give request code for the specified activity ie; startActivityForResult(ActivityB,REQUEST_CODE_FOR_ACTIVITYB). with request code being any integer. Then in onActivityResult check for each request code and implement your logic

like image 33
NIPHIN Avatar answered Sep 28 '22 01:09

NIPHIN