Question: I have my `MainActivity` with `TabLayout` and `ViewPager`.

`TabLayout` have 3 tabs: Tab1, Tab2, Tab3.

In my Tab2 I asked the user to activate the GPS (This really doesn't matter). The point is that I started some intent waiting for the result code or data:

```
status.startResolutionForResult((Activity) parentView.getContext(), REQUEST_CHECK_SETTINGS);

```

So, If I want to call `OnActivityResult` in my `Fragment` Tab2 then I should do something like this in my parent's (`MainActivity`) `onActivityResult`.

```
case ACTIVITY_RESULT_FROM_TAB2:{
                FragmentManager fragmentManager = getSupportFragmentManager();
                Fragment fragment               = fragmentManager.findFragmentById(R.id.main_fragment);
                if(fragment != null){
                    ((Tab2Fragment) fragment).onActivityResult(requestCode, resultCode, data);
                }
            }

```

But it returns cast exception (Tab3 cannot be casted to Tab2) because the last fragment added was Tab3. How can I call `OnActivityResult` from Tab2 properly?

Answer: 
First, i notice the socket closes after the for-loop is existed, and for that to happen, all operations in the loop will have to complete. 
Secondly, the problem could be with the result for item[2] which keeps the function hanging because the:  

if (!$result){  

 $s\_err = socket\_last\_error($this->\_socket);  

 $str\_err = socket\_strerror($s\_err);  

 }  

may not be returning the error captured.. check the two error functions being called. thus,socket\_last\_error($this->\_socket) and socket\_strerror($s\_err).

Use "continue" in the if block to jump any error that may be causing the problem... thus:  

if (!$result){  

 continue;  

 }  

this will jump any error and proceed to the next item in the loop..  

if that works, then the problem will definitely be with the two error functions being called
Hmmm... 6 strings, and you're retrieving 2 strings at a time (`$index` and `$header`) If the `$index` values are numbered from *zero,* and the last one you see is *two,* then the loop has run **(0..1..2) = 3** times which is exactly the right number: **2 \* 3 = 6.** 

But, in general, the easiest way to debug these things is to temporarily add a `print` statement or two, so that you can *see* what's going on.

With PHP, you must always check the logs for "warnings," because the language is notorious for "keeping going" in situations where it ought to stop cold.