Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2. Loeng 2015 Mobiilirakenduste arendamine Android platvormile

Indrek Kõue
May 03, 2015
83

2. Loeng 2015 Mobiilirakenduste arendamine Android platvormile

Indrek Kõue

May 03, 2015
Tweet

Transcript

  1. TOPICS 1. Activity 2. Activity lifecycle 3. Backstack 4. Context

    5. Fragments 6. Intent 7. Intent-Filter 8. Resources 9. Alternative resources
  2. REHEARSAL Gradle = automation tool Software for building software Manages

    dependencies: retrieves from central repo, bundles to you app
  3. REHEARSAL R.java = resource references holder Generated on compile time.

    Example (Java): view.setBackground(R.drawable.minuPilt) textView.setText(R.string.minuTekst) Example (XML): <LinearLayout android:background="@drawable/minuPilt" <TextView android:text="@string/minuTekst"
  4. ACTIVITY Activity = single screen • Each app has to

    have at least 1 Activity • Default Operating System Activity handling: if device is rotate = Activity destroyed • Each Activity needs to be defined in Manifest • Each Activity needs to extend *Activity.java base class public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); } }
  5. ACTIVITY LIFECYCLE State Description Running Activity is visible and interacts

    with the user. Paused Activity is still visible but partially obscured, instance is running but might be killed by the system. Stopped Activity is not visible, instance is running but might be killed by the system. Killed Activity has been terminated by the system of by a call to its finish() method.
  6. OS MEMORY MANAGEMENT Process status Description Priority Foreground An application

    in which the user is interacting with an activity, or which has an service which is bound to such an activity. Also if a service is executing one of its lifecycle methods or a broadcast receiver which runs its onReceive() method. 1 Visible User is not interacting with the activity, but the activity is still (partially) visible or the application has a service which is used by a inactive but visible activity. 2 Service Application with a running service which does not qualify for 1 or 2. 3 Background Application with only stopped activities and without a service or executing receiver. Android keeps them in a least recent used (LRU) list and if requires terminates the one which was least used. 4 Empty Application without any active components. 5
  7. CONTEXT Object which helps us to access system resources (screen

    rendering, writing to disc, network requests etc) Acquired: activity.getContext(); Activity extends Context meaning you can use Activity object where Context is needed (Java polymorphism) Example: meetodiNimi(Context c) Activity a = getActivity(); meetodiNimi(a);
  8. FRAGMENTS Fragment = piece of user interface • Each fragment

    has to extend *Fragment.java • Reusable module • Directly binded to Activity • Lifecycle really similar to Activity (onResume, onPause etc.) Adding to layout • Using object reference and FragmentTransaction in Java • Hardcode to layout XML file
  9. FRAGMENTS // Create new fragment and transaction Fragment newFragment =

    new ExampleFragment(); FragmentTransaction transaction = getFragmentManager(). beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit();
  10. INTENT “An intent is an operation to be performed. “

    Used for: • starting activity • on Brodcast receiving • starting services Example: Intent intent = new Intent(this, ActivitySecond.class); startActivity(intent);
  11. EXPLICIT VS IMPLICIT INTENT EXPLICIT (konkreetne) Intent intent = new

    Intent(this, ActivitySecond.class); startActivity(intent); IMPLICIT (ebamäärane) “Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.” Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage); startActivity(sendIntent);
  12. IMPLICIT INTENT “When startActivity() is called, the system examines all

    of the installed apps to determine which ones can handle this kind of intent (an intent with the ACTION_SEND action and that carries "text/plain" data).”
  13. INTENT-FILTER Defines which 3rd party intents will the application try

    to handle. With intent-filter, your application is saying to OS: I can handle this type of data, you can send it to me if you want <activity android:name="ShareActivity"> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category. DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> </activity>
  14. RESOURCES MyProject/ src/ MyActivity.java res/ drawable/ graphic.png layout/ main.xml info.xml

    values/ strings.xml drawable/ Bitmap files layout/ XML files that define a user interface layout values/ XML files that contain simple values, • arrays.xml for resource arrays • colors.xml for color values • dimens.xml for dimension values. • strings.xml for string values. • styles.xml for styles.
  15. ALTERNATIVE RESOURCES Possible to define alternative resources (images, layouts etc.)

    for different type of configurations (landscape, portrait, high density screen, english UI language etc.) Example: in landscape show ListView + LinearLayout, in portrait only ListView res/ layout/ main.xml layout-land/ main.xml
  16. ALTERNATIVE RESOURCES Most common Qualifiers: • Language and region: en,

    fr etc. • Screen pixel density (dpi): ldpi, mdpi, hdpi, xhdpi etc. • Screen orientation: port, land • Platform Version (API level): v7, v14 Possible to combine: .../res/{type}-port-fr-ldpi/ Note: Qualifier is used in folder name ONLY
  17. ?