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