Back button and refreshing previous activity

Android

Android Problem Overview


If we have two activities:

  1. List of files and last modified time
  2. File editing activity

A user selects a file from the list and is taken to the file editing activity. When done editing, the user presses the back button to return to the file list.

The list is not reloaded and therefore an incorrect value is displayed for the just edited files modified time.

What is the proper method of causing the file list to refresh after the back button is pressed?

This example assumes that no database is being used, just an ArrayAdapter.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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).

Solution 3 - Android

in main:

@Override
public void onRestart()
{
	super.onRestart();
	finish();
	startActivity(getIntent());
}

Solution 4 - Android

I would recommend overriding the onResume() method in activity number 1, and in there include code to refresh your array adapter, this is done by using [yourListViewAdapater].notifyDataSetChanged();

Read this if you are having trouble refreshing the list: https://stackoverflow.com/questions/4088862/android-list-view-refresh/4088889#4088889

Solution 5 - Android

 @Override
protected void onRestart() {
    super.onRestart();
    finish();
    overridePendingTransition(0, 0);
    startActivity(getIntent());
    overridePendingTransition(0, 0);
}

In previous activity use this code. This will do a smooth transition and reload the activity when you come back by pressing back button.

Solution 6 - Android

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
}

Solution 7 - Android

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]

Solution 8 - Android

If not handling a callback from the editing activity (with onActivityResult), then I'd rather put the logic you mentioned in onStart (or possibly in onRestart), since having it in onResume just seems like overkill, given that changes are only occurring after onStop.

At any rate, be familiar with the Activity lifecycle. Plus, take note of the onRestoreInstanceState and onSaveInstanceState methods, which do not appear in the pretty lifecycle diagram.

(Also, it's worth reviewing how the Notepad Tutorial handles what you're doing, though it does use a database.)

Solution 9 - Android

The think best way to to it is using

Intent i = new Intent(this.myActivity, SecondActivity.class); 
startActivityForResult(i, 1);

Solution 10 - Android

Try This

 public void refreshActivity() {
    Intent i = new Intent(this, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish();
    
}

or in Fragment

  public void refreshActivity() {
    Intent i = new Intent(getActivity(), MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish();
    
}

And Add this method to your onBackPressed() like

  @Override
public void onBackPressed() {      
        refreshActivity();
        super.onBackPressed();
    }
}

Thats It...

Solution 11 - Android

@Override
public void onBackPressed() {
    Intent intent = new Intent(this,DesiredActivity.class);
    startActivity(intent);
    super.onBackPressed();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaulView Question on Stackoverflow
Solution 1 - AndroidWaza_BeView Answer on Stackoverflow
Solution 2 - AndroidSarah SakamotoView Answer on Stackoverflow
Solution 3 - AndroidHamze Sheyikh ShoaeiView Answer on Stackoverflow
Solution 4 - AndroidblindstuffView Answer on Stackoverflow
Solution 5 - AndroidRobiul Hossain Shah ImranView Answer on Stackoverflow
Solution 6 - AndroidTô Minh TiếnView Answer on Stackoverflow
Solution 7 - AndroidAizhan NessipbayevaView Answer on Stackoverflow
Solution 8 - AndroidThane AnthemView Answer on Stackoverflow
Solution 9 - AndroidFlavio CandidoView Answer on Stackoverflow
Solution 10 - AndroidRahulView Answer on Stackoverflow
Solution 11 - AndroidSyed ZaidiView Answer on Stackoverflow