Udacity-czego nauczylam sie z kursu – cz.2

1) Activity LifeCycle

https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-practicals/content/images/2_2_P_images/basic_lifecycle.png

a) Zachowywanie stanu aplikacji (np. nr konta bankowego – po obróceniu telefonu;)):

https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras

 

b) AsyncTask i Loaders, Caching (na wypadek obrotu telefonu, gdy sa pobierane dane z sieci/bazy)

https://developer.android.com/guide/components/loaders.html

https://developer.android.com/reference/android/content/AsyncTaskLoader.html

 

https://developer.android.com/training/efficient-downloads/redundant_redundant.html

 

2)Ustawienia użytkownika (zapisywane do pliku):

a)

https://developer.android.com/reference/android/support/v14/preference/PreferenceFragment.html

https://developer.android.com/reference/android/preference/package-summary.html

https://developer.android.com/reference/android/content/SharedPreferences.html

 

https://material.io/guidelines/patterns/settings.html

b) jak sprawdzac poprawność wpisanych danych do preferencji:

 

Step 1: Implement the OnPreferenceChangeListener:

public class SettingsFragment extends PreferenceFragmentCompat implements OnSharedPreferenceChangeListener, Preference.OnPreferenceChangeListener

Step 2: Attach the listener to the size preference in onCreatePreferences

@Override
public void onCreatePreferences(Bundle bundle, String s) {
         /* Other preference setup code code */
        //...
        Preference preference = findPreference(getString(R.string.pref_size_key));
        preference.setOnPreferenceChangeListener(this);
}

Step 3: Implement onPreferenceChange

This code will first try to convert the preference into a number and then checks that the number is between 0 and 3. If either of these fail, a toast will be show and false is returned. By returning false, the incorrect value is not saved to shared preferences.

public boolean onPreferenceChange(Preference preference, Object newValue) {
   Toast error = Toast.makeText(getContext(), "Please select a number between 0.1 and 3", Toast.LENGTH_SHORT);

   String sizeKey = getString(R.string.pref_size_key);
   if (preference.getKey().equals(sizeKey)) {
       String stringSize = ((String) (newValue)).trim();
       if (stringSize == null) stringSize = "1";
       try {
           float size = Float.parseFloat(stringSize);
           if (size > 3 || size <= 0) {
               error.show();
               return false;
           }
       } catch (NumberFormatException nfe) {
           error.show();
           return false;
       }
   }
   return true;
}

 

c) w pliku gradle dodajemy:

 

compile ‚com.android.support:preferencev7:27.0.2′

 

wiecej info o dołaczanych bibliotekach:  https://developer.android.com/topic/libraries/support-library/packages.html

 

3) Aby aktywność potomna miala przycisk ‚Back’

w pliku AndroidManifest.xml

<activity
android:name=".SettingsActivity"
android:parentActivityName=".MainActivity">
    <meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />

w metodzie onCreate:

@Override
publicvoid onCreate(Bundle savedInstanceState){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

 

oraz

 

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}

return super.onOptionsItemSelected(item);
}

 

4) ładne menu w pasku statusu niezajmujące dużo miejsca:

 

plik /res/menu/menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.android.sunshine.DetailActivity">
    <item
android:id="@+id/action_refresh"
android:orderInCategory="1"
android:title="@string/action_refresh"
app:showAsAction="ifRoom"/>


    <item
android:id="@+id/action_settings"
android:orderInCategory="2"
android:title="@string/action_settings"
app:showAsAction="ifRoom"/>

    <item
android:id="@+id/action_map"
android:title="@string/action_map"
app:showAsAction="never" />

 

później odwołujemy się do niego przez:

w aktywności, która ma mieć to menu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    /* Use AppCompatActivity's method getMenuInflater to get a handle on the menu inflater */
MenuInflater inflater = getMenuInflater();
    /* Use the inflater's inflate method to inflate our menu layout to this menu */
inflater.inflate(R.menu.forecast, menu);
    /* Return true so that the menu is displayed in the Toolbar */
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_refresh) {
        invalidateData();
        getSupportLoaderManager().restartLoader(FORECAST_LOADER_ID, null, this);
        return true;
    }

    if (id == R.id.action_map) {
        openLocationInMap();
        return true;
    }


return super.onOptionsItemSelected(item);
}

lub tak: 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.detail, menu);

    Intent intentSettings=new Intent(this,SettingsActivity.class);
    menu.findItem(R.id.action_settings).setIntent(intentSettings);


    MenuItem menuItem = menu.findItem(R.id.action_share);
    menuItem.setIntent(createShareForecastIntent());


    return true;
}

			

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *