Android not executing intent in for loop

时间:2015-09-14 16:01:58

标签: android json android-intent

I'm pulling some JSON responses and depending on wether or not there is a certain object in the response, I want to go to one activity or the other. However, even though my condition for checking id the JSON object is in there works, it is not going to the activity in the if statement. Why is my activity not terminating once it reaches the startActivity in the if statement?

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                JSONObject passingObject = modelsObjectList.get(position);
                ArrayList<String> nextPlacesList = new ArrayList<>();

                try {


                    if(passingObject.optJSONArray("places") == null){
                        Log.v("WE ARE IN THE LOOP", "in the loop");
                        name = passingObject.getString("name");
                        id = passingObject.getLong("id");
                        Log.d("NEXT ACTIVITY NAME", name);
                        System.out.println("NEXT ACTIVITY ID " + id);
                        Log.d("NEXT SERIES LIST", nextPlacesList.toString());

                        Intent intent = new Intent(SeriesActivity.this, SecondSeriesActivity.class);
                        intent.putExtra("id", id);
                        intent.putExtra("name", name);
                        intent.putExtra("placesList", nextPlacesList);
                        startActivity(intent);
                    }

                    Log.v("SHOULDN'T BE HERE", "this is bad");
                    JSONArray places = passingObject.getJSONArray("places");
                    for(int i = 0; i < places.length(); i++){
                        nextPlacesList.add(places.getString(i));

                    }

                    name = passingObject.getString("name");
                    id = passingObject.getLong("id");
                    Log.d("NEXT ACTIVITY NAME", name);
                    System.out.println("NEXT ACTIVITY ID " + id);
                    Log.d("NEXT SERIES LIST", nextPlacesList.toString());

                } catch(JSONException e){
                    Log.d("JSONEXCEPTION", e.getMessage());
                }

                Intent intent = new Intent(SeriesActivity.this, PlacesListActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("name", name);
                intent.putExtra("placesList", nextPlacesList);
                startActivity(intent);
            }
}

1 个答案:

答案 0 :(得分:2)

startActivity doesn't stop the execution of the method. You need to have return before the end of the if block to prevent it from continuing to the SHOULDNT BE HERE section.