I just learned Android programming, and i have a problem.
I create an activity with a checkbox widget that is added programmatically or the checkbox widget will be added if the user touches the add button(tambah), the problem is how to save the state activity?
MainActivity.java
public class MainActivity extends Activity {
// Variable Global
int checkId = 0; //CheckBox Id
EditText ex;
TextView noText;
LinearLayout m;
CheckBox check;
CheckBox noCheck;
String dat;
Toast errorNot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void funcOne(View view) {
/**
* tambah.onClick function
* @param ex - EditText variable
* @param noText - TextView variable used for spacing
* @param m - CheckBox main layout
* @param check - Generated CheckBox widget
* @param noCheck - Toggle between CheckBox and EditText
* @param dat - EditText variable converted to String
* @param errorNot - To display noData error
*/
ex = (EditText)findViewById(R.id.editData);
noText = new TextView(this);
m = (LinearLayout)findViewById(R.id.checkBoxLayout);
check = new CheckBox(this);
noCheck = (CheckBox)findViewById(R.id.noCheck);
dat = ex.getText().toString();
errorNot = Toast.makeText(this, "No input data", Toast.LENGTH_LONG);
// Method
if (dat.length() > 1) {
if (noCheck.isChecked()) {
noText.setText(dat);
m.addView(noText);
} else {
/**
* @param n - New Toast (Only for debugging)
*/
checkId ++;
Toast n = Toast.makeText(this, Integer.toString(checkId), Toast.LENGTH_SHORT);
check.setTag("ch"+checkId);
check.setText(dat + " <WidgetTag " +check.getTag() + ">");
m.addView(check);
n.show();
}
} else {
errorNot.show();
}
}
public void addSpace(View view) {
/**
* space.onClick function
* @param b - Child layout
* @param d - TextView
*/
LinearLayout b = (LinearLayout)findViewById(R.id.checkBoxLayout);
TextView d = new TextView(this);
b.addView(d);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//outState.putBoolean("AstringKey", noCheck);
outState.putString("AStringKey2", dat);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//savedInstanceState.getBoolean("AStringKey");
savedInstanceState.getString("AStringKey2");
}
App Layout: http://imgur.com/gallery/1ZfJ5QL