0

我正在尝试将用户条目保存到表单中,但由于某种原因我无法让它工作。当我终止应用程序并重新启动时,数据不会出现。

我的目标是,如果用户完成表单的一半并且应用程序被终止,那么当应用程序重新启动时,条目仍将保留。任何指导将不胜感激。

public class MainActivity extends Activity {
     public final static String EXTRA_FROM = "com.example.assignment1.FROM";
     public final static String EXTRA_TO = "com.example.assignment1.TO";
     public final static String EXTRA_CC = "com.example.assignment1.CC";
     public final static String EXTRA_BCC = "com.example.assignment1.BCC";
     public final static String EXTRA_SUBJECT = "com.example.assignment1.SUBJECT";
     public final static String EXTRA_COMPOSE = "com.example.assignment1.COMPOSE";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
        EditText emailTo =(EditText) findViewById(R.id.editEmailTo);
        EditText emailCc =(EditText) findViewById(R.id.editEmailCc);
        EditText emailBcc =(EditText) findViewById(R.id.editEmailBcc);
        EditText emailSubject =(EditText) findViewById(R.id.editEmailSubject);
        EditText emailCompose =(EditText) findViewById(R.id.editEmailCompose);

        String from = emailFrom.getText().toString();
        String to = emailTo.getText().toString();
        String cc = emailCc.getText().toString();
        String bcc = emailBcc.getText().toString();
        String subject = emailSubject.getText().toString();
        String compose = emailCompose.getText().toString();

        outState.putString(EXTRA_FROM, from);
        outState.putString(EXTRA_TO, to);
        outState.putString(EXTRA_CC, cc);
        outState.putString(EXTRA_BCC, bcc);
        outState.putString(EXTRA_SUBJECT, subject);
        outState.putString(EXTRA_COMPOSE, compose);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedState)
    {

        EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
        EditText emailTo =(EditText) findViewById(R.id.editEmailTo);
        EditText emailCc =(EditText) findViewById(R.id.editEmailCc);
        EditText emailBcc =(EditText) findViewById(R.id.editEmailBcc);
        EditText emailSubject =(EditText) findViewById(R.id.editEmailSubject);
        EditText emailCompose =(EditText) findViewById(R.id.editEmailCompose);

        String from = savedState.getString(EXTRA_FROM);
        String to = savedState.getString(EXTRA_TO);
        String cc = savedState.getString(EXTRA_CC);
        String bcc = savedState.getString(EXTRA_BCC);
        String subject = savedState.getString(EXTRA_SUBJECT);
        String compose = savedState.getString(EXTRA_COMPOSE);

        emailFrom.setText(from);
        emailTo.setText(to);
        emailCc.setText(cc);
        emailBcc.setText(bcc);
        emailSubject.setText(subject);
        emailCompose.setText(compose);
    }

    public void emailSend (View sendButton) 
    {
        Intent intent = new Intent(this,DisplayEmailActivity.class);
        EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
        EditText emailTo =(EditText) findViewById(R.id.editEmailTo);
        EditText emailCc =(EditText) findViewById(R.id.editEmailCc);
        EditText emailBcc =(EditText) findViewById(R.id.editEmailBcc);
        EditText emailSubject =(EditText) findViewById(R.id.editEmailSubject);
        EditText emailCompose =(EditText) findViewById(R.id.editEmailCompose);

        String from = emailFrom.getText().toString();
        String to = emailTo.getText().toString();
        String cc = emailCc.getText().toString();
        String bcc = emailBcc.getText().toString();
        String subject = emailSubject.getText().toString();
        String compose = emailCompose.getText().toString();

        intent.putExtra(EXTRA_FROM,from);
        intent.putExtra(EXTRA_TO,to);
        intent.putExtra(EXTRA_CC,cc);
        intent.putExtra(EXTRA_BCC,bcc);
        intent.putExtra(EXTRA_SUBJECT,subject);
        intent.putExtra(EXTRA_COMPOSE,compose);

        startActivity(intent);
    }
}
4

2 回答 2

0

当我终止应用程序并重新启动时,数据不会出现。

当然。如果您终止应用程序(例如,任务终止程序),您保存的实例状态就会消失。要处理这种情况,您需要将数据持久化到文件、数据库等。

要测试是否onSaveInstanceState()有效,请旋转屏幕或进行一些其他类型的配置更改。

于 2012-10-10T17:52:40.583 回答
0

要保存一些项目,最好的方法是使用 SharedPreferences。

前段时间我在这篇文章中放了一个例子:SharedPreferences

祝你好运。

于 2012-10-10T18:25:58.937 回答