繁体   English   中英

保存android活动状态

[英]Saving android activity state

我刚学了 Android 编程,遇到了一个问题。

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

主活动.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");
}

应用布局: http : //imgur.com/gallery/1ZfJ5QL

每个活动中的 onCreate() 方法都有一个 Bundle 参数。 您可以在其中保存实例。 您可以在 onPause() 中编写代码,以便在完成活动之前存储您的内容。 您可以使用相同的捆绑包再次访问它

有两种方法可以帮助您实现这一目标。

    @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
            }

此方法允许您的活动即使在重新启动后也能保留其部分数据。 重新启动后要保留的数据可以存储在PersistableBundle对象中。 Bundle对象的使用方式与以下相同。 但是,此方法要求在您启动此活动时设置一些属性,您可以在 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
        }

这种方法只允许你的activity保留一些数据,但是重启后所有的数据都会被清除。 您可以将所有必要的数据存储在Bundle对象中。 下次您返回此活动时,此包将被传递到您的onCreate方法中,前提是您没有销毁活动实例。

但是,您的代码中的错误是这样的;

@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