Reputation: 27105
I want to hide a button in one of my activities if a global data structure does not exist (it's static, in public class Globals extends Application
). Since I want to redraw the button whenever I resume the activity, but would rather not redraw the rest of the view, I put the initialization of the view in onCreate()
and the button-hiding code in onResume()
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myActivity);
}
@Override
protected void onResume() {
super.onResume();
if (Globals.datastructure == null) {
((Button) findViewById(R.id.myButton)).setVisibility(View.GONE);
}
}
When I allocate the data structure and then go back to the activity from a different activity, onResume
is executed correctly but the button does not reappear.
Upvotes: 0
Views: 74
Reputation: 45493
The activity containing the button is probably not being recreated, which means that when you return to it from some other activity, the button is never being set (back) to be visible. You should probably change the onResume()
to something like:
@Override
protected void onResume() {
super.onResume();
findViewById(R.id.myButton).setVisibility(Globals.datastructure == null ? View.GONE : View.VISIBLE);
}
So basically you just have to make sure that whenever Globals.datastructure != null
, you also change the visibility appropriately. In other words: an else
is required with the if
.
Upvotes: 3