20.08.2016 Views

Professional Android 4 Application Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Introducing the Preference Framework and the Preference Activity x 231<br />

saves changes. Note that once again you are creating a new Thread on which to execute the<br />

earthquake refresh code.<br />

@Override<br />

public void onActivityResult(int requestCode, int resultCode, Intent data) {<br />

super.onActivityResult(requestCode, resultCode, data);<br />

if (requestCode == SHOW_PREFERENCES)<br />

if (resultCode == Activity.RESULT_OK) {<br />

updateFromPreferences();<br />

FragmentManager fm = getFragmentManager();<br />

final EarthquakeListFragment earthquakeList =<br />

(EarthquakeListFragment)fm.findFragmentById(R.id.EarthquakeListFragment);<br />

}<br />

}<br />

Thread t = new Thread(new Runnable() {<br />

public void run() {<br />

earthquakeList.refreshEarthquakes();<br />

}<br />

});<br />

t.start();<br />

16. Finally, call updateFromPreferences in onCreate of the Earthquake Activity to ensure the<br />

preferences are applied when the Activity starts:<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

setContentView(R.layout.main);<br />

}<br />

updateFromPreferences();<br />

All code snippets in this example are part of the Chapter 7 Earthquake Part 1<br />

project, available for download at www.wrox.com.<br />

INTRODUCING THE PREFERENCE FRAMEWORK AND THE<br />

PREFERENCE ACTIVITY<br />

<strong>Android</strong> offers an XML-driven framework to create system-style Preference Screens for your applications.<br />

By using this framework you can create Preference Activities that are consistent with those<br />

used in both native and other third-party applications.<br />

This has two distinct advantages:<br />

‰ Users will be familiar with the layout and use of your settings screens.<br />

‰ You can integrate settings screens from other applications (including system settings such as<br />

location settings) into your application’s preferences.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!