I am making an android app in which the user needs to add information. I want if the user left any blank field and clicks on the button the same activity in which other fields were filled should be started.
I am using intents for this and able to start the activity again but all the fields get emptied. How can I do this with intent.
Thanks
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
Register=(ImageButton)findViewById(R.id.register);
Reset=(ImageButton)findViewById(R.id.reset);
firstName=(EditText)findViewById(R.id.firstName);
surName=(EditText)findViewById(R.id.surname);
email=(EditText)findViewById(R.id.eMAil);
password=(EditText)findViewById(R.id.passWord);
retypePassword=(EditText)findViewById(R.id.retypePassword);
town=(EditText)findViewById(R.id.town);
cellno=(EditText)findViewById(R.id.cellno);
dateOfBirth=(Button)findViewById(R.id.datepickerbutton);
Register.setOnClickListener(clickRegisterListener);
private OnClickListener clickRegisterListener = new OnClickListener() {
public void onClick(View v)
{
try{
FirstName=firstName.getText().toString();
SurName=surName.getText().toString();
sex.setOnItemSelec开发者_如何学PythontedListener(new MyOnItemSelectedListener());
Email=email.getText().toString();
Password=password.getText().toString();
RetypePassword=retypePassword.getText().toString();
Town=town.getText().toString();
Cellno=cellno.getText().toString();
if(FirstName.equals(""))
{
AlertDialog.Builder alertbox = new AlertDialog.Builder(Register.this);
alertbox.setMessage("First Name field can not be left empty!");
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{
Intent intent = new Intent(Register.this, Register.class);
// onSaveInstanceState(bundle);
startActivity(intent);
}
});
alertbox.show();
}
What you need to do is pass all information that you want displayed to the activity with the intent. When you create your intent you "put extra" information in the intent.
Intent i = new Intent(this, OtherActivity.class);
i.putExtra("variableName", "variable value");
Then in the activity you can retrieve the information:
Bundle extras = this.getIntent().getExtras();
String var = extras.getString("variableName");
Edit:
If you want to save the activity state when it is is paused you need to save the variables of interest in the onPause() method and then recover them in the onResume() method
You should use startActivityForResult()
.
精彩评论