re-rendering a view using onResume()
When my application loads into an emulator or device, it renders the screen I want it to just fine, including a View with id 'graph'. Suppose I then hit the menu button, and WITHOUT selecting any menu options, hit the back button. The screen no longer shows the View 'graph'.
It is of type View_PieChart, which extends View. Here is the relevant chunk of the XML resource file:
<org.test.View_PieChart
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/graph"
android:background="#000"
/>
Here's the onResume() function I'm usi开发者_JS百科ng. Again, it shows 'graph' when the application initially launched, but once another Activity is launched, then the Back button hit by the user, it fails to draw 'graph'.
protected void onResume() {
super.onResume();
int MaxCount = 100;
View_PieChart graph = (View_PieChart) findViewById(R.id.graph);
Display display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
graph.setGeometry(width, width, 15, 15, 15, 15);
graph.setData(PieData, MaxCount);
}
I've combed through lots of documentation (including the activity lifecycle documentation) but have a feeling that there's something in the View "lifecycle" that I'm not understanding. Any suggestions appreciated.
Did you intend to pass "width" twice in setGeometery()?
Also, could the problem be that at the point onResume() is called, the view layouts have not been completed and so manipulating the geometery does not work as intended. The same issue exists in onCreate() too.
I've had similar problems and found the solution was to create an OnGlobalLayoutListener() that will be called when layout is complete. For example,
mTopView = (RelativeLayout)findViewById(R.id.topView);
mTopView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
// Do here anything that relies on view dimensions being settled
}
});
精彩评论