If we have two activities:
A user selects a file from the list and is taken
in main:
@Override
public void onRestart()
{
super.onRestart();
finish();
startActivity(getIntent());
}
The think best way to to it is using
Intent i = new Intent(this.myActivity, SecondActivity.class);
startActivityForResult(i, 1);
I think onRestart() works better for this.
@Override
public void onRestart() {
super.onRestart();
//When BACK BUTTON is pressed, the activity on the stack is restarted
//Do what you want on the refresh procedure here
}
You could code what you want to do when the Activity is restarted (called again from the event 'back button pressed') inside onRestart().
For example, if you want to do the same thing you do in onCreate(), paste the code in onRestart() (eg. reconstructing the UI with the updated values).
One option would be to use the onResume of your first activity.
@Override
public void onResume()
{ // After a pause OR at startup
super.onResume();
//Refresh your stuff here
}
Or you can start Activity for Result:
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
In secondActivity if you want to send back data:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
if you don't want to return data:
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
Now in your FirstActivity class write following code for onActivityResult()
method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
//Update List
}
if (resultCode == RESULT_CANCELED) {
//Do nothing?
}
}
}//onActivityResult
If you want to refresh previous activity, this solution should work:
In previous activity where you want to refresh:
@Override
public void onRestart()
{
super.onRestart();
// do some stuff here
}
private Cursor getAllFavorites() {
return mDb.query(DocsDsctnContract.DocsDsctnEntry.Description_Table_Name,
null,
null,
null,
null,
null,
DocsDsctnContract.DocsDsctnEntry.COLUMN_Timest);
}
@Override
public void onResume()
{ // After a pause OR at startup
super.onResume();
mAdapter.swapCursor(getAllFavorites());
mAdapter.notifyDataSetChanged();
}
public void swapCursor(Cursor newCursor){
if (mCursor!=null) mCursor.close();
mCursor = newCursor;
if (newCursor != null){
mAdapter.notifyDataSetChanged();
}
}
I just have favorites category so when i click to the item from favorites there appear such information and if i unlike it - this item should be deleted from Favorites : for that i refresh database and set it to adapter(for recyclerview)[I wish you will understand my problem & solution]