Solution 1 :
So as per your question, you want to pass the phone number from the first activity to signup activity you can simply pass it with the help of Intent.
In the first activity after verification simply add this thing
Intent i = new Intent(FirstActivity.this, SignupActivity.class);
i.putExtra("phone",phone.getText().toString().trim()); //add trim at back coz somtime it happen it takes the blank space also
startActivity(i);
In SignUp activity, first of all, get the bundle and afterward check that bundle is null or not like this.
Bundle bundle = getIntent().getExtras();
if(bundle != null){
String phoneNumber = bundle.getString("phone");
}
It is a good practice to check that bundle comes from the previous activity is null or not. If we not check this condition and if bundle comes null than your app will not crash when it load this page.
For reference you can used this link Pass value from one activity to another activity in android
Problem :
I am new to android development so I am facing this issue on my project.
In the first activity, I am first verifying the phone number and after verifying the number, if the user is new, I am sending him to the sign up activity. On the sign up form, I don’t want to put the phone field instead my idea is to directly get the phone number from the first intent and store in the database where I have created a user table to store user data from the sign up form and the phone(in the same table).
I have created sqlite database helper class and a user model class(constructor, getter and setters)
How can I insert or pass the phone field in the user model class object and store it the user table in the database with other data from the sign up form.?
I tried getting the phone intent and passed it as the parameter when calling UserModel class but it’s not working . Or may be I’m doing something wrong.
Please help! Thanks in advance
UserModel Class
package com.example.notes;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class SignUpActivity extends AppCompatActivity {
EditText st_name, sc_name, board, grade, location;
Button schoolBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
st_name = findViewById(R.id.studentId);
sc_name = findViewById(R.id.schoolId);
board = findViewById(R.id.boardId);
grade = findViewById(R.id.gradeId);
location = findViewById(R.id.locationId);
schoolBtn = findViewById(R.id.schoolButton);
schoolBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String phone = getIntent().getStringExtra("mob");
UserModel userModel = new UserModel();
try {
userModel = new UserModel(-1,
st_name.getText().toString(),
sc_name.getText().toString(),
phone,
board.getText().toString(),
grade.getText().toString(),
location.getText().toString());
Toast.makeText(SignUpActivity.this,
userModel.toString(),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(SignUpActivity.this, "Error adding customer", Toast.LENGTH_SHORT).show();
}
DatabaseHelper databaseHelper = new DatabaseHelper(SignUpActivity.this);
boolean success = databaseHelper.addUser(userModel);
Toast.makeText(SignUpActivity.this,
"Success= " + success,
Toast.LENGTH_SHORT).show();
Intent profile_intent = new Intent(SignUpActivity.this,
ProfileActivity.class);
startActivity(profile_intent);
}
});
}
}
DatabaseHelper class
package com.example.notes;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String COL_SCHOOL_TABLE = "SCHOOL_TABLE";
public static final String COL_ID = "ID";
public static final String COL_STUDENT_NAME = "STUDENT_NAME";
public static final String COL_SCHOOL_NAME = "SCHOOL_NAME";
public static final String COL_PHONE = "PHONE";
public static final String COL_BOARD = "BOARD";
public static final String COL_GRADE = "GRADE";
public static final String COL_LOCATION = "LOCATION";
public DatabaseHelper(@Nullable Context context) {
super(context, "Users.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String executeStatement = " CREATE TABLE " + COL_SCHOOL_TABLE + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_STUDENT_NAME + " TEXT, " + COL_SCHOOL_NAME + " TEXT, " + COL_PHONE + " TEXT, " + COL_BOARD + " TEXT, " + COL_GRADE + " TEXT, " + COL_LOCATION + " TEXT)";
db.execSQL(executeStatement);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL(" DROP TABLE IF EXISTS " + COL_SCHOOL_TABLE);
onCreate(db);
}
public boolean addUser(UserModel userModel){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_STUDENT_NAME, userModel.getName());
cv.put(COL_SCHOOL_NAME, userModel.getSchoolName());
cv.put(COL_PHONE, userModel.getPhone());
cv.put(COL_BOARD, userModel.getBoard());
cv.put(COL_GRADE, userModel.getGrade());
cv.put(COL_LOCATION, userModel.getLocation());
long insert = db.insert(COL_SCHOOL_TABLE, null, cv);
if (insert == -1)
return false;
else
return true;
}
public boolean checkPhone(String mobile) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT PHONE FROM SCHOOL_TABLE";
Cursor cur = db.rawQuery(query, null);
cur.moveToFirst();
if (mobile == cur.getString(0))
return true;
else
return false;
}
}