Back button and refreshing previous activity
Asked Answered
D

10

92

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.

Distrain answered 4/4, 2011 at 22:52 Comment(1)
onRestart() should work better. onResume() will be called twice, and onRestart() will be called only when you press the back button.Bannerman
B
44

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

Bannerman answered 12/1, 2014 at 9:8 Comment(3)
This is trivial, but I have to say it: obviously it'd be better to move your onCreate() code into a separate method that you then invoke from both onCreate() as well as onRestart() instead of copy pasting it.Malave
You can also use onCreate() only to inflate views and instantiate global variables and use onStart() to assign/(re)set them. Thus your init() (or whatever you want to call it) will be called whenever the activity starts; i.e. after onCreate() or after onRestart().Motile
This is really useful if you need your activities' ViewModel to be reinitialized. Lets it fetch from database again, setup instance variables...Byproduct
M
39

in main:

@Override
public void onRestart()
{
    super.onRestart();
    finish();
    startActivity(getIntent());
}
Meagre answered 15/12, 2014 at 10:43 Comment(2)
I am guessing this goes to the current activity and not the previous activity?Akers
I had the same issue, except mine was a Fragment not Activity. Thus, I follow advice here and it worked: #35040012Fuchs
E
8

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: Android List view refresh

Everard answered 4/4, 2011 at 22:56 Comment(0)
M
6
 @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.

Markham answered 12/5, 2020 at 14:48 Comment(2)
Can you add some description to the code and thank you for the post.Electrotechnology
this works fine, but it is not so smooth a normal transition between activities, but can live with thatStott
M
2

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
}
Mokpo answered 27/11, 2017 at 22:42 Comment(0)
H
1
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]

Heloise answered 17/8, 2017 at 8:57 Comment(1)
consider adding some explainationCthrine
V
0

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

Vigesimal answered 5/4, 2011 at 2:13 Comment(0)
S
0

The think best way to to it is using

Intent i = new Intent(this.myActivity, SecondActivity.class); 
startActivityForResult(i, 1);
Schaab answered 20/2, 2015 at 12:18 Comment(0)
C
0

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

Chipper answered 13/8, 2018 at 10:24 Comment(0)
D
-1
@Override
public void onBackPressed() {
    Intent intent = new Intent(this,DesiredActivity.class);
    startActivity(intent);
    super.onBackPressed();
}
Decalcify answered 24/9, 2019 at 21:37 Comment(2)
This question is some years old and the provided answer was flagged for review as a Low Quality Post. Here are some guidelines for How do I write a good answer?. There are other answers that provide the OP's question, and they were posted some time ago. When posting an answer, please make sure you add either a new solution, or a substantially better explanation, especially when answering older questions.Particularism
onBackPressed is deprecatedStott

© 2022 - 2024 — McMap. All rights reserved.