refresh activity on back button in android

Normally, when user click the back button in android, the next view is not refreshed. I had this problem when I was doing the settings. if user changed the some settings and hit back button, the new settings won’t be applied to the “back” list view since all the cells are reused.
you can do some scroll to refresh the list view but that is definitely not something we wanna user to do.
first solution is to override the onResume method in the back view, but it is not guaranteed to work in listView.
However the following code should do the work:

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && isNightModeToggled) {
            Intent a = new Intent(this,MainActivity.class);
            return true;
        return super.onKeyDown(keyCode, event);

the “MainActivity” is my next view, replaced it with you own “next” view.



  1. Marco · September 4, 2014

    isNightModeToggled what is?

    • LEON · September 4, 2014

      Never mind. That is my own boolean, just remove it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s