i am working on an android app that uses an sqlite db to containing info that the user inputs. i have two activities. currently the main activity displays the user info from the db. the second activity is intended only for input of that data. when the second activity is opened i see the saved data in the edittext boxes. i can modify and save the data. when returning to the first activity however, the data displayed there does not update. if i close the app and restart it the new data will be displayed in the first activity correctly, but i need it to show instantly. i have tried overriding onResume() thinking this would help. but so far it hasn't helped me. please help me find the problem. here is my main activity, the first activity:
public class LaunchActivity extends AppCompatActivity{
final String SCOPE ="https://www.googleapis.com/auth/gmail.send";
private static final int REQUEST_RESOLVE_AUTH_ERROR = 1006;
private static final int REQUEST_SMS = 645;
private ImageView mImageView;
private TextView nameField;
private TextView phoneField;
private TextView emailField;
private ClientInfoDBHelper mInfoDBHelper;
private Cursor mCursor;
private String mPhoneNumber = "**********";
private String[] permissions = new String[]{Manifest.permission.SEND_SMS, Manifest.permission.READ_PHONE_STATE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nameField = (TextView) findViewById(R.id.nameView);
phoneField = (TextView) findViewById(R.id.phoneView);
emailField = (TextView) findViewById(R.id.emailView);
mInfoDBHelper = new ClientInfoDBHelper(this);
SQLiteDatabase dbReader = mInfoDBHelper.getReadableDatabase();
String[] projection = {
ClientInfoContract.ClientInfoEntry.FIRST_NAME,
ClientInfoContract.ClientInfoEntry.LAST_NAME,
ClientInfoContract.ClientInfoEntry.PHONE_NUMBER,
ClientInfoContract.ClientInfoEntry.EMAIL_ADDRESS
};
String where = ClientInfoContract.ClientInfoEntry._ID + " = ?";
String[] whereArgs = {"1"};
String sortOrder = ClientInfoContract.ClientInfoEntry.FIRST_NAME + " DESC";
mCursor = dbReader.query(
ClientInfoContract.ClientInfoEntry.TABLE_NAME,
projection,
where,
whereArgs,
null,
null,
sortOrder
);
mCursor.moveToNext();
updateDisplayInfo(mCursor);
//request permissions
int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion> Build.VERSION_CODES.LOLLIPOP_MR1){
if (hasPermission()){
ActivityCompat.requestPermissions(this, permissions, REQUEST_SMS);
}
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Are you certain you'd like to alert your attorney?", Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.snack_bar_send, new View.OnClickListener(){
@Override
public void onClick(View v){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mPhoneNumber, null, "testing", null, null);
new MailTask().execute();
}
}).show();
}
});
mImageView = (ImageView) findViewById(R.id.imageView);
mImageView.setImageDrawable(getResources().getDrawable(R.drawable.no_handcuffs));
}
@Override
public void onResume(){
super.onResume();;
updateDisplayInfo(mCursor);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_launch, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id){
case R.id.action_settings:
//null
break;
case R.id.userInfo:
Intent infoIntent = new Intent(this, UserInfoActivity.class);
startActivity(infoIntent);
break;
}
return super.onOptionsItemSelected(item);
}
private boolean hasPermission(){
int result = ContextCompat.checkSelfPermission(this, permissions[0]);
if (result==PackageManager.PERMISSION_DENIED){
return true;
}else {
return false;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
//temporarily null// may be used in future to handle multiple permissions
}
public void updateDisplayInfo(Cursor curse){
//curse.moveToNext();
StringBuilder sb = new StringBuilder();
sb.append(curse.getString(curse.getColumnIndex(ClientInfoContract.ClientInfoEntry.FIRST_NAME)))
.append(" ")
.append(curse.getString(curse.getColumnIndex(ClientInfoContract.ClientInfoEntry.LAST_NAME)));
nameField.setText(sb.toString());
}
}
Are you coming back from your second activity to first activity? Ideally onCreate()
should be use only to initialize data or attaching to views. If your data is changing you should do the stuff in onStart()
or onResume()
So add the below code in your onStart()
method and remove from onCreate()
.
mInfoDBHelper = new ClientInfoDBHelper(this);
SQLiteDatabase dbReader = mInfoDBHelper.getReadableDatabase();
String[] projection = {
ClientInfoContract.ClientInfoEntry.FIRST_NAME,
ClientInfoContract.ClientInfoEntry.LAST_NAME,
ClientInfoContract.ClientInfoEntry.PHONE_NUMBER,
ClientInfoContract.ClientInfoEntry.EMAIL_ADDRESS
};
String where = ClientInfoContract.ClientInfoEntry._ID + " = ?";
String[] whereArgs = {"1"};
String sortOrder = ClientInfoContract.ClientInfoEntry.FIRST_NAME + " DESC";
mCursor = dbReader.query(
ClientInfoContract.ClientInfoEntry.TABLE_NAME,
projection,
where,
whereArgs,
null,
null,
sortOrder
);
mCursor.moveToNext();
updateDisplayInfo(mCursor);
onCreate()
is only called once when your view is getting created. And then in your onResume()
you are trying to use the same cursor so you will not get the updated data. So you will have to query again to get the updated data.
Or you can do startActivityForResult()
and in onActivityResult()
method update your data by firing fresh query.