简体   繁体   中英

onResume never been called when click the phone hardware back button on android?

I'm facing this problem I have MainActivity and SettingsPreference so i want to reload adapter on MainActivity when i return from settings to MainActivity i used onResume() it worked vary good when i click on ActionBar back button, but when i try it on hardware back button it it never been called,i find out that the system calls on onPostResume() instead of onResume() so try to use it, but the changes are never been applied

this is the SettingsActivity i'm using

This code works when the ActionBar back button is pressed

      @Override
          protected void onResume() {
              super.onResume();
              dataFromSharedPrefrence = getSettingsSharedPreferences();
              adapterExample.notifyDataSetChanged();

          }

The system calls onPostResume() when hardware back button is pressed but the changes never applied

@Override
        protected void onPostResume() {
              super.onPostResume();
              dataFromSharedPrefrence = getSettingsSharedPreferences();
              adapterExample.notifyDataSetChanged();
}

This is the SettingsActivity

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.settings, new SettingsFragment())
                .commit();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }


    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }

I find out this difference when i Override lifecycle methods

  1. When ActionBar button is pressed this is the output
2020-05-05 06:00:44.899 23467-23467/com.example.test E/MainActivity: onDestroy
2020-05-05 06:00:44.907 23467-23467/com.example.test E/MainActivity: onDetachedFromWindow
2020-05-05 06:00:44.964 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:44.990 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.009 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.015 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.037 23467-23467/com.example.test E/MainActivity: onContentChanged
2020-05-05 06:00:45.048 23467-23467/com.example.test E/MainActivity: onStart
2020-05-05 06:00:45.051 23467-23467/com.example.test E/MainActivity: onPostCreate
2020-05-05 06:00:45.053 23467-23467/com.example.test E/MainActivity: onResume
2020-05-05 06:00:45.056 23467-23467/com.example.test E/MainActivity: onPostResume
2020-05-05 06:00:45.078 23467-23467/com.example.test E/MainActivity: onAttachedToWindow
2020-05-05 06:00:45.141 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.188 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.223 23467-23467/com.example.test E/MainActivity: onCreateView
  1. when hardware back button is pressed this is the output

2020-05-05 06:01:21.732 23467-23467/com.example.test E/MainActivity: onRestart
2020-05-05 06:01:21.734 23467-23467/com.example.test E/MainActivity: onStart
2020-05-05 06:01:21.738 23467-23467/com.example.test E/MainActivity: onResume
2020-05-05 06:01:21.739 23467-23467/com.example.test E/MainActivity: onPostResume

Try this:

Instead of using onResume() try onStart() :

  @Override
      protected void onStart() {
          super.onStart();
          dataFromSharedPrefrence = getSettingsSharedPreferences();
          adapterExample.notifyDataSetChanged();

      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM