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:

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

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

Advertisements

2 comments

  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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s