Alternative to direct onResume() call

  • A+
Category:Languages

I am rewriting my Android app to eliminate direct calls to onResume().

My app currently does most of its work inside onResume() it then posts the display and that is the end of onResume().

@Override public void onResume() {     super.onResume();     // get current date and time,     //  and determine if daylight savings time is in effect.     //...600 lines of code     // output both the chart and report     //     image.setImageBitmap(heightChart);     report.setImageBitmap(reportBitmap); } 

The next step is gathering user input, which tells me what changes to the report the user wishes. (It may be a new location, a new date, or new display style, etc). This is done as follows:

@Override public boolean onOptionsItemSelected(MenuItem item) {     final int STATION_REQUEST = 1;     int test = 1;     switch (item.getItemId()) {         case R.id.refresh: {             userDateSet = false;             onResume();             return true;         } // come to the present.          //...200 lines of code         default:             return super.onOptionsItemSelected(item);     } } 

As the example shows, the output is regenerated by calling onResume() after the new user command is determined. THIS IS BAD PRACTICE, I ALREADY KNOW!! Yet it works well as far as I have determined, I honestly do not understand the problem with it.

My solution in mind is to gather the 600 lines of code into a separate routine and call that instead, both from within onResume() and numerous points within onOptionsItemSelected()

@Override public void onResume() {     super.onResume();     myOnResumeCode(); } 

And inside onOptionsItemSelected() do this

@Override public boolean onOptionsItemSelected(MenuItem item) {     final int STATION_REQUEST = 1;     int test = 1;     switch (item.getItemId()) {         case R.id.refresh: {             userDateSet = false;             myOnResumeCode();             return true;         } // come to the present.      ... // Other statements } 

Is this method acceptable? If not, any suggestions short of "rewrite the whole thing" will be very helpful to me. I have searched extensively for a clean solution but not found one I can understand. Thank You.

 


Yet it works well as far as I have determined, I honestly do not understand the problem with it.

You are assuming that calling super.onResume() is appropriate in cases where you are manually calling onResume(). That is not a safe assumption.

Is this method acceptable

It is certainly an improvement and well worth doing.

600 lines of code is a really long method. You would fail many code reviews, with reviewers asking you to refactor that code to be more maintainable. Also, depending on what you are doing in those 600 lines it may be better to move that logic to a background thread.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: