Resume using the Back button
I have an app that I am creating using DashboardActivity and SettingsActivity. I have one object displayed on the toolbar, but when I go into settings I want to be able to select / deselect X. As soon as the user clicks the Back button, I save this data locally and on the server. As soon as the phone receives a message that the server has shut down successfully, I want to restart the dashboard.
I thought I would do it with onPause and onResume, but gets called when the DashboardActivity is first created. What would be the best way to reload the dashboard by calling my web service after saving the settings to the server? This is what I do when the back button is clicked
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Save data to the server once the user hits the back button
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
SchoolSearchActivity.this.divisionProxy = new DivisionProxy(SchoolSearchActivity.this.saveUserDivisionHandler);
SchoolSearchActivity.this.divisionProxy.submitUserDivisions(SchoolSearchActivity.this.userDivisions, SchoolSearchActivity.this.user_id);
//SchoolSearchActivity.this.finish();
//Toast.makeText(SchoolSearchActivity.this, "Divisions Saved",Toast.LENGTH_LONG).show();
}
return true;
}
The above opens an HTTP connection and when a response is received, the handler processes the response. I want to: 1) send data to server 2) return to previous activity 3) show progress dialog until response from # 1 returns (response handler is in settings activity and now we are in dashboard activity) 4) "refresh" dashboard.
What is the best way to do this so that I can just resume the toolbar. As I said, when I overwrite the onResume method, it gets called when the activity is first created. I was considering putting my webservice call into onResume, so it would call once every time you do it in an Activity, but that doesn't seem like the cleanest way.
What do you suggest for 1-4?
a source to share
You can subclass Application
and set a flag to indicate that your current cached data is dirty. You create your own subclass Application
to hold the state of the transient global application, which is exactly what you want here. When the user changes their configuration in SettingsActivity
, you set a flag at the application level, and then in onResume()
you can check the flag and only call the web service when you know that the user has changed something.
Alternatively, if you can set DashboardActivity
singleTop
the startup mode , then use the call startActivity()
to navigate from SettingsActivity
to DashboardActivity
if the settings have changed. If DashboardActivity
- singleTop
, then a new instance will not be created, but a new one Intent
will be passed to the method onNewIntent()
and you can reload your settings there.
a source to share