I got Activity, in onCreate() I try to fetch data:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
...............
fillUI();
}
public void getDetailedItem(){
FeedParser parser=new FeedParser();
try{
mItem=parser.parseDetailed();
}catch(Exception e){
closeAndShowError();
}
}
public void fillUI(){
getDetailedItem();
if(mItem!=null){
...............
}else{
closeAndShowError();
}
}
private void closeAndShowError(){
Context context = getApplicationContext();
CharSequence text = getResources().getString(R.string.toast_error);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
finish();
}
Them problem is, when Exception occures, it is caught, and I expect my activity to finish itself, but I still get code inside fillUI() executed, so I had to put if statement after getDetailedItem();
How do I solve it?
You can do something like this:
Then in
fillUI()ifmItemisnullyou show’s error;