I have a class called HeaderView which I use all over my application:
public HeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
commonApi = AAALifestyleApplication.commonApi;
user = commonApi.getCurrentUser();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.header_view, this, true);
((ImageView)this.findViewById(R.id.logo)).setOnClickListener(this);
inboxButton = (ImageView) this.findViewById(R.id.inbox_image);
inboxButton.setOnClickListener(this);
inboxButton2 = (ImageView) this.findViewById(R.id.inbox_image2);
inboxButton2.setOnClickListener(this);
requestsButton = (ImageView) this.findViewById(R.id.requests_image);
requestsButton.setOnClickListener(this);
requestsButton2 = (ImageView) this.findViewById(R.id.requests_image2);
requestsButton2.setOnClickListener(this);
progressBar = (ProgressBar) this.findViewById(R.id.header_progress_bar);
if (!showProgressBar) {
progressBar.setVisibility(INVISIBLE);
}
AAAAsyncTask.setProgressBarListener(this);
refreshView();
}
public static void refreshView(){
SharedPreferences sp = ctx.getSharedPreferences("HeaderView", Context.MODE_PRIVATE);
int newMessages = sp.getInt("newMessagesCount", 0);
int newRequests = sp.getInt("newRequestsCount", 0);
if(newMessages > 0){
Log.d("daim", "new messages!");
inboxButton.setVisibility(View.GONE);
inboxButton2.setVisibility(View.VISIBLE);
}
else{
Log.d("daim", "no new messages!");
inboxButton.setVisibility(View.VISIBLE);
inboxButton2.setVisibility(View.GONE);
}
if(newRequests > 0){
requestsButton.setVisibility(View.GONE);
requestsButton2.setVisibility(View.VISIBLE);
}
else{
requestsButton.setVisibility(View.VISIBLE);
requestsButton2.setVisibility(View.GONE);
}
}
In my Activity, this HeaderView gets called onCreate cause its specified in the XML, and also in onResume() like this:
@Override
public void onResume(){
super.onResume();
HeaderView.refreshView();
}
In the onCreate() method this works correctly, but onResume() I get the right amount of messages and I even logged this, so I know for sure that “no new messages” is printed, but the setVisibility() method doesn’t update at all, and instead I see the previous ImageView when I had “new messages”.
Please help, I’ve tried using handler with a thread to see if that was the problem, but it still remains this way.
You should instantiate your view, and make the
refreshViewnon-static.Declare your
Activitylike this:And remove the
statickeyword in yourrefreshViewmethod declaration.