문제

In my Android application I'm using the ActionBar. I want to show users some status using logo placed in the left side of the ActionBar. I can do this:

ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.new_icon);

and it works.

The problem is that this state in to persistent. If I change activity ActionBar reset itself, so default logo is shown. Is there any way to save state of the ActionBar during application runtime?

도움이 되었습니까?

해결책

Use the standar lifecycle callbacks from Activities

In the OnCreate method of your activity try to read the state

public void onCreate(Bundle icicle) {
      if (data!=null)
      {
          ActionBar actionBar = getSupportActionBar();
          int state = data.getInt("status");
          if (state==1)
               actionBar.setIcon(R.drawable.icon_a);
          else
               actionBar.setIcon(R.drawable.icon_b);
      }
}

And save the state to recovery it later

protected void onSaveInstanceState(Bundle data) {
      super.onSaveInstanceState(data);
      data.putInt(icon_state);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top