Back button and updating previous activity

If we have two actions:

  • File List and Last Time Change
  • File Editing Actions

The user selects a file from the list and switches to file editing activity. After editing, the user clicks the "Back" button to return to the list of files.

The list does not reload, and therefore the changed time of the files just edited is displayed.

What is the proper method of updating the file list after clicking the back button?

This example assumes the database is not in use, just an ArrayAdapter.

+81
android
Apr 04 2018-11-11T00:
source share
8 answers

One option is to use onResume of your first action.

@Override public void onResume() { // After a pause OR at startup super.onResume(); //Refresh your stuff here } 

Or you can run Activity for Result:

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

In SecondActivity, if you want to send back the data:

  Intent returnIntent = new Intent(); returnIntent.putExtra("result",result); setResult(RESULT_OK,returnIntent); finish(); 

if you do not want to return data:

 Intent returnIntent = new Intent(); setResult(RESULT_CANCELED, returnIntent); finish(); 

Now in your FirstActivity class write the following code for the 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 
+100
Apr 04 2018-11-11T00:
source share

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 can specify what you want to do when the action is restarted (again called by pressing the back button) inside onRestart ().

For example, if you want to do the same as onCreate (), paste the code in onRestart () (for example, restore the user interface with updated values).

+37
Jan 12 '14 at 9:08
source share

mostly:

 @Override public void onRestart() { super.onRestart(); finish(); startActivity(getIntent()); } 
+32
Dec 15 '14 at 10:43
source share

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

Read this if you have problems updating the list: Updating Android List View

+8
Apr 04 2018-11-11T00:
source share

If you want to update previous activity, this solution should work:

In the previous step in which you want to update:

 @Override public void onRestart() { super.onRestart(); // do some stuff here } 
+2
Nov 27 '17 at 22:42
source share

If you don’t handle the callback from the editing action (using onActivityResult), I would prefer to put the logic that you talked about onStart (or, possibly, onRestart), since its presence in onResume just seems redundant, given that the changes only after onStop.

In any case, familiarize yourself with the life cycle of activity . Also, pay attention to the onRestoreInstanceState and onSaveInstanceState methods, which do not appear on the diagram of a fairly life cycle.

(It’s also worth considering how the Notepad Tutorial handles what you do, although it uses a database.)

0
Apr 05 2018-11-11T00:
source share

Best to think that he uses

 Intent i = new Intent(this.myActivity, SecondActivity.class); startActivityForResult(i, 1); 
0
Feb 20 '15 at 12:18
source share
 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 have only the category of favorites, so when I click on an item from my favorites, this information appears, and if I, unlike it, this item should be removed from Favorites: for this I update the database and install it in the adapter (for recyclerview) [I want you to understand my problem and solution]

0
Aug 17 '17 at 8:57
source share



All Articles