I'm developing a simple android flashlight application and when I rotate the screen I want to save previous status I have several buttons which change the color of layout and when I touch the screen buttons appear and disappear. but as I told I need to preserve last status I got the idea that I should do it with onSaveInstanceState and onRestoreInstanceState but unfortunately I do not know how to implement the code. here is the code that I wrote :
// import com.uncocoder.course.app.startup4.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class FlashlightActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button Black = (Button) findViewById(R.id.Black);
final Button White = (Button) findViewById(R.id.White);
final Button Yellow = (Button) findViewById(R.id.Yellow);
final Button Red = (Button) findViewById(R.id.Red);
final Button Green = (Button) findViewById(R.id.Green);
final LinearLayout backLayout = (LinearLayout) findViewById(R.id.backLayout);
final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
Black.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
backLayout.setBackgroundColor(Color.parseColor("#000000"));
Toast.makeText(FlashlightActivity.this, "Black Color Set", Toast.LENGTH_SHORT).show();
}
}); //end black
White.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
backLayout.setBackgroundColor(Color.parseColor("#ffffff"));
Toast.makeText(FlashlightActivity.this, "White Color Set", Toast.LENGTH_SHORT).show();
}
}); //end white
Yellow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
backLayout.setBackgroundColor(Color.parseColor("#ffff00"));
Toast.makeText(FlashlightActivity.this, "Yellow Color Set", Toast.LENGTH_SHORT).show();
}
}); //end yellow
Red.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
backLayout.setBackgroundColor(Color.parseColor("#ff0000"));
Toast.makeText(FlashlightActivity.this, "Red Color Set", Toast.LENGTH_SHORT).show();
}
}); //end red
Green.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
backLayout.setBackgroundColor(Color.parseColor("#a4c639"));
Toast.makeText(FlashlightActivity.this, "Green Color Set", Toast.LENGTH_SHORT).show();
}
}); //end green
backLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (Black.getVisibility() == View.GONE) {
Black.setVisibility(View.VISIBLE);
} // end if
else {
Black.setVisibility(View.GONE);
}// end else
if (White.getVisibility() == View.GONE) {
White.setVisibility(View.VISIBLE);
} // end if
else {
White.setVisibility(View.GONE);
}// end else
if (Yellow.getVisibility() == View.GONE) {
Yellow.setVisibility(View.VISIBLE);
} // end if
else {
Yellow.setVisibility(View.GONE);
}// end else
if (Red.getVisibility() == View.GONE) {
Red.setVisibility(View.VISIBLE);
} // end if
else {
Red.setVisibility(View.GONE);
}// end else
if (Green.getVisibility() == View.GONE) {
Green.setVisibility(View.VISIBLE);
} // end if
else {
Green.setVisibility(View.GONE);
}// end else
}// end public void
}); // end seton
}
}