简体   繁体   中英

android preferencefragment mysteriously called twice

The first time my android app is launched I bring up a preferencefragment because the user needs to initially make a selection from a list of items and their selection needs to be saved. I extend ListPreference to display the list of items.

The way it should work is the user selects an item from the presented list. This selection gets saved in SharedPreferences and the app transitions to another fragment. All of this is working, but for some reason the ListPrefence gets display a second time.

I've put in some logging and discovered that for some reason two ListPreference object are being constructed...but I know of only one call to create it. I'm somehow missing where/why the ListPreference constructor is called a second time.

My PreferenceFragment code is simple. It looks like

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Log.i("INIT", "ONCREATE");
  addPreferencesFromResource(R.xml.inititems); // load from XML

There's only the addPreferencesFromResource() call. No other place in my fragment code should (as far as I understand) cause the ListPreference to be instancitated, but the logging tells me the ListPreference constructor is called twice and the fragment's onCreate is called just once.

I'm going to try to do a stack trace from the ListPreference constructor, but I wanted to see if anyone here has thoughts or suggestions beyond the stack trace. Why would the constructor be called twice ?

Thanks! -Mar

You possibly have referenced the fragment in your layout resource file using android:name=" yourfragment " as well as at run-time in onCreate(). That would cause it to be loaded twice.

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