ユーザーがアクティビティに戻ったときに入力されたコンテンツが入力されたままになるように、フォームに入力された値を保存しようとしています。今は機能しているので。ユーザーは詳細を入力して「送信ボタン」をクリックできます。次のアクティビティでは、ユーザーが入力した内容の詳細が表示されます。ユーザーが「戻るボタン」を選択すると、ユーザーは前のアクティビティを返しますが、フォームのフィールドからコンテンツが消去されます。任意の提案をいただければ幸いです。
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("");
}
}
2番目のアクティビティ......
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);
}
}