简体   繁体   中英

Android:How to save values in EditText when returning to an Activity

I am trying to save values entered to a form so that when a user returns to the activity the content entered remains populated. As it functions now. The user can enter details and click the "send button". The the next activity will then show details of what the user entered. If the user selects the "back button", the user returns the previous activity but the content is wiped from the fields in the form. Any suggestions would be appreciated.

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

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_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);
        String from = emailFrom.getText().toString();
        outState.putString(EXTRA_FROM, from);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedState)
    {
        EditText emailFrom =(EditText) findViewById(R.id.editEmailFrom);
        String from = savedState.getString(EXTRA_FROM);
        emailFrom.setText(from);
    }
    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 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 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_SUBJECT,subject);
        intent.putExtra(EXTRA_COMPOSE,compose);

        startActivity(intent);
    }



    public void emailClear (View clearButton) {
         ((EditText) findViewById(R.id.editEmailFrom)).setText("");
         ((EditText) findViewById(R.id.editEmailTo)).setText("");
         ((EditText) findViewById(R.id.editEmailCc)).setText("");
         ((EditText) findViewById(R.id.editEmailBcc)).setText("");
         ((EditText) findViewById(R.id.editEmailSubject)).setText("");
         ((EditText) findViewById(R.id.editEmailCompose)).setText("");  
    }

}

Second Activity......

public class DisplayEmailActivity extends Activity {

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

        Intent intent = getIntent();

        String from = intent.getStringExtra(MainActivity.EXTRA_FROM);
        String to = intent.getStringExtra(MainActivity.EXTRA_TO);
        String cc = intent.getStringExtra(MainActivity.EXTRA_CC);
        String subject = intent.getStringExtra(MainActivity.EXTRA_SUBJECT);
        String compose = intent.getStringExtra(MainActivity.EXTRA_COMPOSE);


        TextView textFrom =(TextView)findViewById(R.id.displayEmailFrom);
        TextView textTo =(TextView)findViewById(R.id.displayEmailTo);
        TextView textCc =(TextView)findViewById(R.id.displayEmailCc);
        TextView textSubject =(TextView)findViewById(R.id.displayEmailSubject);
        TextView textCompose =(TextView)findViewById(R.id.displayEmailCompose);

        textFrom.setText(from);
        textTo.setText(to);
        textCc.setText(cc);
        textSubject.setText(subject);
        textCompose.setText(compose);

    }

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

    public void emailBack (View backButton){
        Intent intent = new Intent(this,MainActivity.class);

        startActivity (intent);
    }


}

emailBack starts a whole new activity. just call finish instead

Use Shared preferences... To create a String value use -

String string12 = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getString("butname12", " "); 

And if you want to change it or rewrite -

getSharedPreferences("PREFERENCE", MODE_PRIVATE)
                    .edit()
                    .putString("butname12", "0" )
                    .commit(); 

To call the meaning of saved value -

String ss = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getString("butname12", " ");

Replace the method like this,

public void emailBack (View backButton){

    onBackPressed();
}

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