简体   繁体   English

保存android活动状态

[英]Saving android activity state

I just learned Android programming, and i have a problem.我刚学了 Android 编程,遇到了一个问题。

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?我创建了一个带有以编程方式添加的复选框小部件的活动,或者如果用户触摸添加按钮(tambah),将添加复选框小部件,问题是如何保存状态活动?

MainActivity.java主活动.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应用布局: http : //imgur.com/gallery/1ZfJ5QL

There is a Bundle parameter for onCreate() method in each activity.每个活动中的 onCreate() 方法都有一个 Bundle 参数。 You can save instance in that.您可以在其中保存实例。 You can write code in onPause() so that before finishing activity it will store your content.您可以在 onPause() 中编写代码,以便在完成活动之前存储您的内容。 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.重新启动后要保留的数据可以存储在PersistableBundle对象中。 The Bundle object is used in the same way as the one below. Bundle对象的使用方式与以下相同。 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.但是,此方法要求在您启动此活动时设置一些属性,您可以在 android sdk 文档中阅读更多相关内容。

     @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.这种方法只允许你的activity保留一些数据,但是重启后所有的数据都会被清除。 You can store all your necessary data in the Bundle object.您可以将所有必要的数据存储在Bundle对象中。 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.下次您返回此活动时,此包将被传递到您的onCreate方法中,前提是您没有销毁活动实例。

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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