简体   繁体   中英

Saving android activity state

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

There is a Bundle parameter for onCreate() method in each activity. You can save instance in that. You can write code in onPause() so that before finishing activity it will store your content. You can again access it by using same bundle

There are 2 methods available that can help you achieve this.

    @Override
            public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
                super.onSaveInstanceState(outState, outPersistentState);
// save your data into either or both of the parameters with the put### methods
            }

This method allows your activity to retain some of its data even after reboot. The data that you want to retain after reboot can be stored in the PersistableBundle object. The Bundle object is used in the same way as the one below. This method however requires that some attributes be set on when you are starting this activity, you can read more on this in the android sdk documentation.

     @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
// save your data into either or both of the parameters with the put### methods
        }

This method only allows your activity to retain some data, but all the data will be cleared after a reboot. You can store all your necessary data in the Bundle object. This bundle will then be passed into your onCreate method the next time you return to this activity, provided you didn't destroy the activity instance.

The mistake in your code however is this;

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    //savedInstanceState.getBoolean("AStringKey");
    savedInstanceState.getString("AStringKey2");// you are not assigning this to the dat variable.
// it should rather be
dat = savedInstanceState.getString("AStringKey2");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM