How to Refresh a Previous Activity After the Back Button Is Pressed

How can I refresh a previous activity after the back button is pressed?

You should probably override your onResume() to check if the list of alarms have been refreshed so that whenever you come to/come back to your ListActivity, it'll get updated.

Refresh activity on back button

onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.

You should load comments (api/server call) in onResume function rather than onCreate function of Activity A so that every time the activity is resumed your comments are refreshed.

How can I restart activity on back button pressed?

You could just override the onBackButton pressed in your second activity to start the first one. Example:

@Override
public void onBackPressed() {
startActivity(new Intent(this, FirstActivity.class));
}

refresh fragment that gone to activity after back pressed in that activity

Let's say you have two two activities A and B.Initially Fragment f1 is added to A.When you click on item in f1 start activity B with startActivityForResult (Intent intent, int requestCode) and in onActivityResult(int requestCode, int resultCode, Intent data) either update the fragment data(if possible) or reload the fragment.

How to go back and refresh the previous page in Flutter?

You can trigger the API call when you navigate back to the first page like this pseudo-code

class PageOne extends StatefulWidget {
@override
_PageOneState createState() => new _PageOneState();
}

class _PageOneState extends State<PageOne> {
_getRequests()async{

}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new RaisedButton(onPressed: ()=>
Navigator.of(context).push(new MaterialPageRoute(builder: (_)=>new PageTwo()),)
.then((val)=>val?_getRequests():null),
),
));
}
}

class PageTwo extends StatelessWidget {
@override
Widget build(BuildContext context) {
//somewhere
Navigator.pop(context,true);
}
}

Or you can just use a stream if the API is frequently updated, the new data will be automatically updated inside your ListView

For example with firebase we can do this

stream: FirebaseDatabase.instance.reference().child(
"profiles").onValue

And anytime you change something in the database (from edit profile page for example), it will reflect on your profile page. In this case, this is only possible because I am using onValue which will keep listening for any changes and do the update on your behalf.

After pressing back button, previous activity (listview) is emtpty

try this on AllStudents activity

@Override
protected void onResume() {
super.onResume();
if (studentList != null && studentList.size()>0) {
ListAdapter adapter = new ListAdapter(studentList, this);
StudentListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}

*Note

when you make a search you run the filterStudents() method

and replace the current list in the adapter

ListAdapter adapter = new ListAdapter(filteredList, this);

but make sure that if you search for nothing .. return the original list

ListAdapter adapter = new ListAdapter(studentList, this);

or you will get an empty list view



Related Topics



Leave a reply



Submit