Check validation and pass value from edit text to move activity - java

I have a LoginActivity that is not tied to any online database, I only validate the username via edit text component to login and move to the MainActivity. LoginActivity appears when the value of text in MainActivity is null, therefore I created a validation method when the username is null, then the layout will move to LoginActivity, I made it like this with the aim that LoginActivity does not become the main layout of the app.
Validation users in MainActivity.java:
private void checkUsername() {
Intent intent = getIntent();
if(intent.getExtras() != null){
String users = intent.getStringExtra(LoginActivity.EXTRA_USER);
txuser.setText(users);
//NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String)
ContentValues cv = new ContentValues();
cv.put(Contract.UserEntry.NAME_COLUMN,users);
database.insert(Contract.UserEntry.TABLE_NAME,null,cv);
} else {
startActivity(new Intent(this, LoginActivity.class));
}
}
LoginActivity.java
private void loginUser() {
final String username;
username = user.getText().toString();
if (TextUtils.isEmpty(username)) {
Toast.makeText(this, "Name cannot be blank!", Toast.LENGTH_SHORT).show();
}
if (!cbAgree.isChecked()) {
Toast.makeText(this, "Login fails!", Toast.LENGTH_SHORT).show();
}
fabLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, UtamaActivity.class);
intent.putExtra(EXTRA_USER, username);
startActivity(intent);
}
});
}
How do I fix this?, because when I open the app for the first time, the LoginActivity is successfully displayed, but when I enter to MainActivity it immediately forces close. I have included the logcat comment in the checkUsername() method

Change Intent intent = new Intent(LoginActivity.this, UtamaActivity.class);
to Intent intent = new Intent(LoginActivity.this, MainActivity.class);

Related

Android Studio: Activity only finishes when you call the finish() for second time

So, we're working with intents at school and I'm having trouble with the intents when I try to pass data from the "Activity2" to the "Activity1", when I do the setResult() and stuff. The problem is it won't go back to the first activity when I trigger the event the first time, but it will the second.
I've been working with Android studio only for about 12h so I really lack a lot of understanding.
Here is what I'm doing:
First I call this form the main activity.
public void CheckPassword(View view) {
password = PasswordManagement.getPassword(this);
TextView txtPassword = findViewById(R.id.txtPassword);
if (txtPassword.getText().toString().equals(password)) {
Intent intent;
intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("password", password);
startActivityForResult(intent, 1);
startActivity(intent);
} else {
Intent intent;
intent = new Intent(this, RestrictedActivity.class);
startActivityForResult(intent, 1);
startActivity(intent);
}
}
Then, when I'm done from the second activity I run this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restricted);
lblRestrictedArea = findViewById(R.id.lblRestrictedArea);
lblRestrictedArea.setOnLongClickListener(
new OnLongClickListener() {
public boolean onLongClick(View view) {
intent = new Intent();
intent.putExtra(EXTRA_RESPONSE, true);
setResult(RESULT_OK, intent);
finish();
return false;
}
});
}
And back to the main activity I overwrote this to act according to the response:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
if (data.getBooleanExtra(RestrictedActivity.EXTRA_RESPONSE,false)){
LinearLayoutPasswordActivity.setBackgroundColor(getResources().getColor(R.color.red));
}else{
LinearLayoutPasswordActivity.setBackgroundColor(getResources().getColor(R.color.white));
}
}
}
}
If anyone can help I would be very glad, meanwhile I'll try to solve it my own.
Thanks!
Your are calling startActivity twice. So there are two instance of the same Activity and then you have to finish twice.
Keep your startActivityForResult(...) and delete startActivity in CheckPassword(View view)
->
public void CheckPassword(View view) {
password = PasswordManagement.getPassword(this);
TextView txtPassword = findViewById(R.id.txtPassword);
if (txtPassword.getText().toString().equals(password)) {
Intent intent;
intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("password", password);
startActivityForResult(intent, 1);
// startActivity(intent);
} else {
Intent intent;
intent = new Intent(this, RestrictedActivity.class);
startActivityForResult(intent, 1);
//startActivity(intent);
}
}
Plus, note that you are using the same requestCode (1) for two different activities. The requestCode is very important for onActivityResult method.

Intent passing variable malfunction

In my app I am trying to provide two functions to the users. One is uploading a post with an image and the other is to upload post without an image. To do so I created Strings nonselected_image and selected_image (or selected_bitmap). In case of pressing one of the TextViews by user one of the strings will be passed within the intent consequently. It works great when user wishes to create non photo post but when he wants to create one with a photo there are two posts emerging, one with photo and the other without. What did I wrong? Here is my code.
Sending an intent and strings to the next activity
TextView nextScreen = (TextView) view.findViewById(R.id.tvNext);
nextScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to the final share screen.");
if (isRootTask()) {
Intent intent = new Intent(getActivity(), NextActivity.class);
intent.putExtra(getString(R.string.selected_image), mSelectedImage);
startActivity(intent);
} else {
Intent intent = new Intent(getActivity(), AccountSettingsActivity.class);
intent.putExtra(getString(R.string.selected_image), mSelectedImage);
intent.putExtra(getString(R.string.return_to_fragment), getString(R.string.edit_profile_fragment));
startActivity(intent);
getActivity().finish();
}
}
});
noPhoto.setOnClickListener(new View.OnClickListener(){
public void onClick (View v) {
mSelectedImage = null;
Intent intent = new Intent(getActivity(), NextActivity.class);
intent.putExtra(getString(R.string.nonselected_image), nonSelectedImage);
startActivity(intent);
}
});
receiving an intent and taking actions accordingly to passed variable
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to the final share screen.");
//upload the image to firebase
// Toast.makeText(NextActivity.this, "Attempting to upload new photo", Toast.LENGTH_SHORT).show();
String caption = mCaption.getText().toString();
if(intent.hasExtra(getString(R.string.selected_image))){
imgUrl = intent.getStringExtra(getString(R.string.selected_image));
Toast.makeText(NextActivity.this, "Attempting to upload a normal photo", Toast.LENGTH_SHORT).show();
mFirebaseMethods.uploadNewPhoto(getString(R.string.new_photo), caption, imageCount, imgUrl,null);
}
else if(intent.hasExtra(getString(R.string.selected_bitmap))){
bitmap = (Bitmap) intent.getParcelableExtra(getString(R.string.selected_bitmap));
Toast.makeText(NextActivity.this, "Attempting to upload a new bitmap", Toast.LENGTH_SHORT).show();
mFirebaseMethods.uploadNewPhoto(getString(R.string.new_photo), caption, imageCount, null,bitmap);
}
else if(intent.hasExtra(getString(R.string.nonselected_image)));{
Toast.makeText(NextActivity.this, "Attempting to upload new photo", Toast.LENGTH_SHORT).show();
imgUrl = intent.getStringExtra(getString(R.string.nonselected_image));
mFirebaseMethods.uploadNoPhoto(getString(R.string.new_photo), caption, imageCount, imgUrl, null); // HERE IS THE ERROR
}

change value of string from other activity in java

I have two classes Class "A" and Class "B". In class A i have declared a
String password ="admin" .Now in class B i am taking value from user and i
want to change that value of string in class A and want to store value that
user entered. How can i do that? Any help please?
class A
forgetpassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent j = new Intent(MainActivity.this,newpassword.class);
startActivity(j);
Class B
EditText newpassword;
Button change;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_password);
newpassword = (EditText)findViewById(R.id.newpassword);
change = (Button)findViewById(R.id.chnage);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.b= newpassword.getText().toString();
Intent y= new Intent(NewPassword.this,MainActivity.class);
startActivity(y);
}
});
Use StartActivityForResult()
When you intend to get some result back from an Activity you should start activity using startActivityForResult(intent,requestcode)
Its easy Follow these steps
StartActivity
Intent i = new Intent(MainActivity.this, ForgotPasswordActivity.class);
startActivityForResult(i, 1);
Set new password in ForgotPasswordActivity
Now you are in ForgotPasswordActivity, set your new password and return back to MainActivity on Button click like this.
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent y= new Intent(NewPassword.this,MainActivity.class);
y.putExtra("someKey",newpassword.getText().toString()); //set new password
setResult(Activity.RESULT_OK, returnIntent); // set your result
finish(); // return back to MainActivity
}
});
Retrive new Password in MainActivity
Now you navigated back to MainActivty to retrieve your new password override onActivityResultMethod().
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String newPassword=data.getStringExtra("someKey");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
Here a link on startActivityForResult()
You can send data from one activity to another in the intent, Use the putExtra() methods the intent class to pass data.Send the data like this
Intent y= new Intent(NewPassword.this,MainActivity.class);
y.putExtra("yourKey",newpassword.getText().toString())
startActivity(y);
and in the onCreate() of MianActivity you can retrieve the value using getStringExtra("yourKey") like this
Intent callingIntent = getIntent();
String password = callingIntent.getStringExtra("yourKey");
and use the password.
Or you can start the NewPassword activity for a result and get the data back in onActivityResult()
Intent newPasswordIntent = new Intent(this,NewPassword.class);
startActivityForResult(newPasswordIntent ,REQUEST_CODE);
and in the change button click handler,
Intent intent= new Intent();
intent.putExtra("yourKey",newpassword.getText().toString());
setResult(Activity.RESULT_OK, intent);
finish();
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String password = data.getStringExtra("yourKey")
}
Public static variables are easy to call, be aware that they can cause memory leaks
This can lead to context leaking or NPE when you finish the activity.
Instead use an Application class and initialize the string variable in it and with the use of getters() and setters() you can access the variable.
public class BaseApplication extends android.app.Application{
private String admin;
#Override
onCreate(...){
this.admin = "";
}
public String getAdmin(){
return this.admin;
}
public void setAdmin(String admin){
this.admin = admin;
}
}
classA:
...
((BaseApplication) getApplicationContext()).setAdmin("!mpr355!v3");
...
classB:
...
String myAdmin = ((BaseApplication) getApplicationContext()).getAdmin();
...
Also in your manifest file.
<application
android:name=".BaseApplication"
` ...>
UPDATE
Before startActivity(y);
just call
y.putExtra("pass", password);
and in your MainActivity, call getIntent().getStringExtra("pass");
Check for null pointers and you are good to go! :)
Muhammad,
There are many ways to do that and some of the listing here,
Use Broadcast receiver
Use start activity for result
Use static variable
From all of this if you want to change much time then I think broadcast receiver is good one,
Here is the example of broadcast receiver,
In your activity A register broadcast receiver with specific action,
private BroadcastReceiver brodcastRec=new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent!=null && intent.getAction().equals("changePassword")){
String newPassword=intent.getStringExtra("newPassword");
//do whatever you want to do here after getting new password
}
}
};
And then after write following code in your on create of activity A
IntentFilter intentFilter=new IntentFilter("changePassword");
registerReceiver(brodcastRec,intentFilter);
And In your activity B or click event of Button
Intent intent=new Intent();
intent.setAction("changePassword");
intent.putExtra("newPassword","yourValue");
sendBroadcast(intent);

How to keep login user on an application in Android

I have an app that requires user to register.
User opens the app, registers and logs in.
Does some stuff with the app and closes it.
Opens the app again, the user is recognized so he doesn't need to login again.
This is the code used for login:
ArrayList<ArrayList<Object>> data = dm.getAllRowsAsArrays();
for (int position=0;position<data.size();position++)
{
ArrayList<Object> row = data.get(position);
mail1=row.get(0).toString();
pass1=row.get(2).toString();
if((mail1.equals(mail))&&(pass1.equals(pass)))
{
Toast.makeText(getApplicationContext(),"Login Success",Toast.LENGTH_LONG).show();
Intent i=new Intent(getApplicationContext(),usermain.class);
startActivity(i);
}
}
After login goto usermain activity, but if I press the emulator back button then I can see the login page with the details I typed for login.
How can I remove it?
You can use startActivityForResult instead of startActivity and then use this sample onActivityResult method in your login activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == LOGIN_CODE) { // this code you should provide in startActivityForResult
this.finish();
}
}
This will prevent you from going back to login activity. If you'd like to persist user's data, use also SharedPreferences as described by others.
public class SaveSharedPreference
{
static final String PREF_USER_NAME= "username";
static SharedPreferences getSharedPreferences(Context ctx) {
return PreferenceManager.getDefaultSharedPreferences(ctx);
}
public static void setUserName(Context ctx, String userName)
{
Editor editor = getSharedPreferences(ctx).edit();
editor.putBoolean(PREF_USER_NAME, userName);
editor.commit();
}
public static boolean getUserName(Context ctx)
{
return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
}
in your main activity check this first:
if(SaveSharedPreference.getUserName(MainActivity.this).length() == 0)
{
// call Login Activity
}
else
{
// Call Next Activity
}
Use SharedPreference for that in your onCreate() methos like..
SharedPreferences sp=getSharedPreferences("pref",0);
if(sp.getInt("PREFERENCES_LOGIN", 99)==0)
{
setContentView(R.layout.activity_login_screen);
}else if(sp.getInt("PREFERENCES_LOGIN", 99)==1)
{
Intent intent=new Intent(GetLoginDetailsScreen.this,MenuScreen.class);
startActivity(intent);
finish();
super.onBackPressed();
}else{
setContentView(R.layout.activity_login_screen);
}
after login Successful add this..
sp=getSharedPreferences("pref",0);
SharedPreferences.Editor edit= sp.edit();
edit.putInt("PREFERENCES_LOGIN", 1);
edit.commit();
try this
if((mail1.equals(mail))&&(pass1.equals(pass)))
{
Toast.makeText(getApplicationContext(),"Login Success",Toast.LENGTH_LONG).show();
Intent i=new Intent(getApplicationContext(),usermain.class);
finish();
startActivity(i);
}
so here is your solution when the user successfully logged in set the shared preferences in your login class class like this
//declare pref editor
SharedPreferences prefs;
SharedPreferences.Editor prefsEditor;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefsEditor = prefs.edit();
//paste below peace of code when the loginwill be success
prefsEditor.putString("remember", "yes");
prefsEditor.commit();
//now in your first activity you just check the shared pref value to know the user is logged in or not
SharedPreferences prefs;
String Log;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
Log=prefs.getString("remember", "");
//now check the value of shared pref and apply the condition like this
Intent intent ;
if(register.equalsIgnoreCase("yes"))
{
intent = new Intent(this, Home.class);
startActivity(intent);
finish();
}
else
{
intent = new Intent(this, Login.class);
startActivity(intent);
finish();
}

How to use startActivityForResult() through a DialogFragment?

My app requires a username to be added to function properly.
The mainActivity displays the username on the top which it retrieves form the database.
The mainActivity also has a button which leads to 'addusername' activity through startActivityForResult() method; when the user actually enters a username then only username on the main activity gets refreshed and displayed.
There is a submit button on the 'addusername' activity which adds the username and does setResult(RESULT_OK);
Everything works fine if the username is entered by clicking the 'addusername' button.
Now, I have added a dialogue in mainActivity which gets displayed when the app starts only if no username exists on the database. The dialogue gives an option to add username, which if clicked, leads to the 'addusername' activity. But upon pressing the 'submit' button the mainActivity does get called but the username is not refreshed (though, the database does get updated)
Here is the code for 'addusername' activity:
public void submitusername(View view) {
userdatabase name = new userdatabase(this);
name.open();
String user = name.getusername();
name.close();
EditText cinone = (EditText) findViewById(R.id.username);
username = cinone.getText().toString();
if(user.equals("")) {
userdatabase entry = new userdatabase(Editprofile.this);
entry.open();
entry.createEntry(username, null, null, null);
entry.close();
Context context = getApplicationContext();
CharSequence text = "Added new user!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else {
userdatabase update = new userdatabase(Editprofile.this);
update.open();
update.updateUsername(username);
update.close();
Context context = getApplicationContext();
CharSequence text = "Username updated!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
setResult(RESULT_OK);
finish();
}
Here is the code for Dialog:
public class NouserFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.nouseralert)
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getActivity().getBaseContext(), Editprofile.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.ignore, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
I understand this is happening because the Dialog does not call startActivityForResult() method. But even if I change my code, like this:
Intent intent = new Intent(getActivity().getBaseContext(), Editprofile.class);
startActivityForResult(intent, 0);
It still does not help. Probably because, onActivityResult() method does not lie in the same class from which startActivityForResult() has been called, but I don't know how to get after that.
EDIT 1: Tried using
Intent intent = new Intent(getActivity(), Editprofile.class);
startActivityForResult(intent, 0);
No use.
Have you tried this way:
Intent intent = new Intent(getActivity(), Editprofile.class);
startActivityForResult(intent, 0);
When you create your the Intent object you use the BaseContext, instead of using the reference to the activity itself.
Using
Intent intent = new Intent(getActivity(), Editprofile.class);
getActivity().startActivityForResult(intent, 0);
worked!

Categories