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
I used
SharedPreferences
instead ofintent.putExtra
at AddModifyTask
Probably, your problem is here:
You are creating
intent1
forAddModifyTask
, but startingintent
forHomeActivity
.Try something like: