Reputation: 213
I am new to android app development and I have the following problem when I pass a parameter through activities. In my app the user enters his email, username and password, clicks the submit button and then he goes to the next activity (NewProfile.class) where his username should be printed in a TextView. But when the user fills in all fields correctly and the toast "Welcome to Allamoda" is shown, each field become red (this happens only when user enters something wrong) and then he returns to MainActivity.
Signup.class code:
package dmst.allamoda;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class Signup extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
setupVariables();
}
private EditText email;
private EditText username;
private EditText password1;
private EditText password2;
public static String PUBLIC_STATIC_STRING_IDENTIFIER = "";
public void signupControl(View view) {
if ((email.getText().toString().trim().length() > 0) && (username.getText().toString().trim().length() > 0) && (password1.getText().toString().trim().length() > 0) &&
(password2.getText().toString().trim().length() > 0) && (password1.getText().toString().equals(password2.getText().toString()))) {
Toast.makeText(getApplicationContext(), "Welcome to AllaModa!",
Toast.LENGTH_SHORT).show();
Intent resultIntent = new Intent(this, NewProfile.class);
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, username.getText().toString());
setResult(NewProfile.RESULT_OK, resultIntent);
finish();
} else {
if (!(email.getText().toString().trim().length() > 0)) {
Toast.makeText(getApplicationContext(), " email!",
Toast.LENGTH_SHORT).show();
}
if (!(username.getText().toString().trim().length() > 0)) {
Toast.makeText(getApplicationContext(), " username!",
Toast.LENGTH_SHORT).show();
}
if (!(password1.getText().toString().trim().length() > 0)) {
Toast.makeText(getApplicationContext(), " password!",
Toast.LENGTH_SHORT).show();
}
if (!(password2.getText().toString().trim().length() > 0)) {
Toast.makeText(getApplicationContext(), " password!",
Toast.LENGTH_SHORT).show();
} else {
if (!(password1.getText().toString().equals(password2.getText().toString()))) {
Toast.makeText(getApplicationContext(), "passwords!",
Toast.LENGTH_SHORT).show();
}
}
}
email.setBackgroundColor(Color.parseColor("#F5CCCC"));
email.setTextColor(Color.parseColor("#B80000"));
username.setBackgroundColor(Color.parseColor("#F5CCCC"));
username.setTextColor(Color.parseColor("#B80000"));
password1.setBackgroundColor(Color.parseColor("#F5CCCC"));
password1.setTextColor(Color.parseColor("#B80000"));
password2.setBackgroundColor(Color.parseColor("#F5CCCC"));
password2.setTextColor(Color.parseColor("#B80000"));
}
private void setupVariables() {
email = (EditText) findViewById(R.id.signupEmail);
username = (EditText) findViewById(R.id.signupUsername);
password1 = (EditText) findViewById(R.id.signup1Password);
password2 = (EditText) findViewById(R.id.signup2Password);
}
}
Upvotes: 0
Views: 61
Reputation: 66967
Signup
is not transitioning to the NewProfile
activity. It is returning to the prior activity (presumably, MainActivity
).
This is the problemmatic code:
Intent resultIntent = new Intent(this, NewProfile.class);
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, username.getText().toString());
setResult(NewProfile.RESULT_OK, resultIntent);
finish();
Presumably, you want to do something like this:
Intent intent = new Intent(this, NewProfile.class);
intent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, username.getText().toString());
startActivity(intent);
finish();
[EDITED]
The finish();
statement at the end should keep the Signup
activity out of the history stack in more modern devices. However, some commenters to this answer (@AndrewBreen and @Cookster) indicate that doing so may not work on slower devices.
If you are worried about this, @Cookster's comment presents a possible solution to this that avoids the call to finish()
:
The pattern I use to avoid this is to have the activity that opened the login activity act in a routing capacity in onActivityResult i.e. it checks login state, if not valid then opens login activity, otherwise does actions that required auth state etc.
Upvotes: 2
Reputation: 271
try this on activity a
Intent i = new Intent(this, ActivityB.class);
i.putExtra("SomeKEY",YourData);
On activity B in the oncreate method
Bundle extras = getIntent().getExtras();
if(extras !=null)
if(extras !=null) {String value = extras.getString("KEY");}
Upvotes: 0