Save state of ActionBar visibility

Last exercise demonstrate how to "Show and hide ActionBar using Java code". But, once configuration changed (such as orientation change), ActionBar will return to setting in AndroidManifest.xml. To resume the user set visibility of ActionBar, we can save it in onSaveInstanceState() and restore it in onCreate().

Please notice that it can save state for configuration change only: if the app is killed or finished, then re-started, it will return to AndroidManifest.xml set visibility.

Save state of ActionBar visibility


package com.example.androidactionbar;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonToggleActionBar = (Button)findViewById(R.id.toggleactionbar);
buttonToggleActionBar.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
ActionBar myActionBar = getActionBar();
boolean myActionBarisShowing = myActionBar.isShowing();
if(myActionBarisShowing){
myActionBar.hide();
Toast.makeText(getApplicationContext(),
"Hide ActionBar",
Toast.LENGTH_LONG).show();
}else{
myActionBar.show();
Toast.makeText(getApplicationContext(),
"Show ActionBar",
Toast.LENGTH_LONG).show();
}
}});

Button buttonFinish = (Button)findViewById(R.id.finish);
buttonFinish.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
finish();
}});

if (savedInstanceState != null) {
Boolean savedActionBarisShowing =
savedInstanceState.getBoolean("KEY_ACTIONBAR_ISSHOWING", true);
if(savedActionBarisShowing){
getActionBar().show();
}else{
getActionBar().hide();
}
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("KEY_ACTIONBAR_ISSHOWING", getActionBar().isShowing());
}

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/toggleactionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toggle ActionBar" />
<Button
android:id="@+id/finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="finish()" />

</LinearLayout>