skip to Main Content

I need to get the account from MainActivity to add to my command in AddModifyTask. I used intent.putExtra in MainActivity and get account by getIntent.getStringExtra in AddModifyTask but it always return null, still the same with getIntent.getStringExtra in Fragment then it returns the correct account value, can anyone help me please

MainAcitivy, I need to pass account from here

Boolean result = db.checkUserNamePassword(account, password);
                    if(result==true) {
                        Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                        intent.putExtra("account", account);
                        Intent intent1 = new Intent(MainActivity.this, AddModifyTask.class);
                        intent1.putExtra("account", account);
                        startActivity(intent);

AddModifyTask activity where I need to pass account to but it just return null

public void saveTask(View v) {
        /*Checking for Empty Task*/
        if (edit_text.getText().toString().trim().length() > 0) {
            if (isModify) {
                String account = getIntent().getStringExtra("account");
                mydb.updateTask(task_id, edit_text.getText().toString(), new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()), account);
                Toast.makeText(getApplicationContext(), "Task updated.", Toast.LENGTH_SHORT).show();
            } else {
                String account = getIntent().getStringExtra("account");
                mydb.insertTask(edit_text.getText().toString(), new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()), account);
                Toast.makeText(getApplicationContext(), "Added.", Toast.LENGTH_SHORT).show();
            }
            finish();

        } else {
            Toast.makeText(getApplicationContext(), "No empty.", Toast.LENGTH_SHORT).show();
        }

still the same with getIntent.getStringExtra in Fragment then it returns the correct account value

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_profile, container, false);
        TextView textViewAccount = v.findViewById(R.id.textViewAccount);

        String account = getActivity().getIntent().getStringExtra("account");
        textViewAccount.setText(account);
        }
        return v;
    }

Function insertTask and updateTask in database, is the problem with the statement in the database or the getIntent in the AddModifyTask or in both?

public boolean insertTask(String task, String task_at, String account) {
    Date date;
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("task", task);
    contentValues.put("task_at", task_at);
    contentValues.put("status", 0);
    contentValues.put("user", account);
    db.insert(TB_TASK, null, contentValues);
    return true;
}

    public boolean updateTask(String id, String task, String task_at, String account) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("task", task);
        contentValues.put("task_at", task_at);
        contentValues.put("user",  account);
        db.update(TB_TASK, contentValues, "id = ? ", new String[]{id});
        return true;
    }

Please help!

2

Answers


  1. Chosen as BEST ANSWER

    I used SharedPreferences instead of intent.putExtra

    Boolean result = db.checkUserNamePassword(account, password);
                        if(result==true) {
                            Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                            intent.putExtra("account", account);
                            SharedPreferences sharedPref = getSharedPreferences("account", MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPref.edit();
                            editor.putString("account", account);
                            editor.apply();
                            startActivity(intent);
    

    at AddModifyTask

    public void saveTask(View v) {
            /*Checking for Empty Task*/
            if (edit_text.getText().toString().trim().length() > 0) {
                if (isModify) {
                    SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
                    String account = sharedPreferences.getString("account","");
                    mydb.updateTask(task_id, edit_text.getText().toString(), new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()), account);
                    Toast.makeText(getApplicationContext(), "Tác vụ đã được cập nhật.", Toast.LENGTH_SHORT).show();
                } else {
                    SharedPreferences sharedPreferences = getSharedPreferences("account", MODE_PRIVATE);
                    String account = sharedPreferences.getString("account","");
                    mydb.insertTask(edit_text.getText().toString(), new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()), account);
                    Toast.makeText(getApplicationContext(), "Đã thêm.", Toast.LENGTH_SHORT).show();
                }
                finish();
    

  2. Probably, your problem is here:

    Intent intent = new Intent(MainActivity.this, HomeActivity.class);
    intent.putExtra("account", account);
    Intent intent1 = new Intent(MainActivity.this, AddModifyTask.class);
    intent1.putExtra("account", account);
    startActivity(intent);
    

    You are creating intent1 for AddModifyTask, but starting intent for HomeActivity.

    Try something like:

    Intent intent = new Intent(MainActivity.this, AddModifyTask.class);
    intent.putExtra("account", account);
    startActivity(intent);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search