tags:

views:

167

answers:

1

I have an application I am creating with a DashboardActivity & a SettingsActivity. On the dashboard, I have one object displayed, but when I go into settings, I want to be able to select/deselect X options. Once the user clicks the back button, I save that data locally and to the server. Once the phone receives a success message from the server that it was stored properly, I want to reload the dashboard.

I thought I would do this with the onPause and onResume, but they are called when the DashboardActivity is first created. What would be the best way to reload the dashboard by calling my web service after the settings were saved to the server? Here is what I am doing when the back button is hit

@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 the response is received, the handler processes the response. I want to: 1) submit the data to the server 2) return to the previous activity 3) show a progress dialog until the response from #1 returns (response handler is in the settings activity; and we are now in the dashboard activity) 4) "refresh" the dashboard

What is the best way to accomplish this so I can just resume the dashboard as it was. Like I said, when I overwrite the onResume method, it is called when the activity is first created. I was considering putting my webservice call in onResume, so it's called once everytime you make it to the Activity, but it doesn't seem like the cleanest way.

What would you suggest for 1-4?

+1  A: 

You could subclass Application and set a flag to show that you're current cached data is "dirty". You create your own subclass of Application to store transient global application state which is exactly what you want here. When the user changes their configuration in SettingsActivity you set the flag at the Application level and then in onResume() you can check the flag and only call the web service when you know the user has changed something.

Alternatively, when you could set your DashboardActivity to have a singleTop launch mode and then use a startActivity() call to move from SettingsActivity to DashboardActivity if settings have changed. If the DashboardActivity is singleTop then a new instance will not be created and the new Intent will be passed to the onNewIntent() method and you can reload your settings there.

Dave Webb
subclassing Application looks like what I need. Do you have any links that show how others might use this in a similar situation? I have read about people storing states in Application class, just haven't seen any good examples. Thanks!
Hallik