Wednesday, August 3, 2016

CHP3_Multiple Activities and Intents


  • Multiple activities
    • Most of the Apps have more than one activities.
  • Intent
    • Let Activities talk to each others
    • Across the apps boundary , interact with other apps in the same device.

============================================================================================================================================

Activity def what user can do (just one thing) ! 

Steps to build the App
  1. app structure
  2. build project
  3. update layout
    1. <Button>
    2. android : onClick ="onSendMessage"
    3. android : text = "@string/send"/>
  4. update strings.xml
    1. <string name = "send">Send Message</string>
  5. New method for activity
    1. public void onSendMessage (View view) {  }
  6. The 2nd Activity and layout
    1. File > New > Activity / Blank Activity / ReceiveMessageActivity ; activity_receive_message > finish
  7. AndroidManifest.xml
  8. Intent
    1. import android.content.Intent
    2. Intent intent = new Intent (this , ReceiveMessageActivity.class);
    3. startActivity(intent);
  9. Sending Text to the 2nd Intent
    1. Layout
      1. <TextView> ID
      2. Delete hello world
    2. Activity
      1. putExtra()
        1. intent.putExtra("message",value);
          1. message => name of the string
          2. overloaded
          3. value => can be different types
      2. getIntent();
        1. String string = intent.getStringExtra("message");
        2. int intNum = intent.getIntExtra("name,default_value");
  10. Specific action's intent

============================================================================================================================================
  • New GUI components 
    • Spinner
    • EditText
      • can edit text column
      • can input text
      • inherit from Android View (Class)
    • TextView
    • Button
  • String Resources
    • strings.xml
  • Responsive Web Design
    • em as unit
============================================================================================================================================

  • AndroidManifest.xml
    • app/src/main
    • include app significant info
      • what activity include in the app (MAIN , LAUNCHER)
      • which libraries and declaration(declare activity exist in application element)we've  used 
      • package name
      • default app icon
      • app theme
      • security permissions....
    • if you're not using Android Studio IDE , you've to build it yourself.
  • Intent
    • Build Intent => Intent intent = new Intent (this , Target.class)
      • mail address (sender , receiver)
    • Launch Intent Activity => startActivity(intent)
    • Not Found => ActivityNotFoundException
============================================================================================================================================

  • Why 2 activities rather than 1 activity handle all of the stuff
    • coz the back button
    • the order issue

============================================================================================================================================

  • Changing App , sending messages to others
    • How does Android App works ?
      • Android App , is combined by multiple Activities and other components
    • activity1 > intent > Android (check) > intent >  activity2 (even though in other app) 
  • How do we know which apps on the device?
    • "action" can solve all the problem
============================================================================================================================================
  • Explicit intent
    • directly tell Android what class
  • Implicit intent.
    • which activity let Android decide
============================================================================================================================================

establish specific action's intent 
  • Intent intent -= new Intent (action)
    • action
      • Intent.ACTION_DIAL
      • Intent.ACTION_WEB_SEARCH
      • Intent.ACTION_SEND
        • extra message adding
          • intent.setType("text/plain")
          • intent.putExtra(Intent.EXTRA_TEXT , messageText) ;
          • intent.putExtra(Intent.EXTRA_SUBJECT, subject);

============================================================================================================================================

" intent filter " tell Android which activity can handle the action

  • Intent resolution
    • explicit intent
      • it's easy 
    • implicit intent
      • Android
        • app > AndroidManifest.xml > check intent filter to accomplish the work
  • Intent filter
      • indicate what  components can receive what types of intent 
    • action
      • what activity can do ?
    • category
      • provide activity extra informations
        • main entry pt or not ?

for implicit intent
  • android.intent.category.DEFAULT
  • intent filter
    • compare to app > AndroidManifest.xml
      • action
      • MIME

============================================================================================================================================

run app on reality device
  1. USB debugging
    1. Developer options
    2. Settings > About Phone > Click build number *7 times
    3. Windows > install USB driver (http://developer.android.com/tools/extras/oem-usb.htmo)
    4. USB cable to connect to your PC (allow to use USB debug) RSA key
  2. createChooser()
============================================================================================================================================

Quick Recap

  • Task
    • >=2 activities get together
  • <EditText>
    • Edit text ,input text 
    • inherit from Android View
  • Android Studio > File > New... > Activity (for new activity)
  • AndroidManifest.xml
  • intent
    • a tool let Android elements can talk to each others
  • explicit intent
    • Intent intent = new Intent(this , Target.class);
  • start activity
    • startActivity(intent) 
    • ActivityNotFoundException
  • add more info to intent
    • putExtra()
  • get intent to launch activity
    • getIntent()
  • get relative info about intent
    • get*Extra()
      • getStringExtra()
      • getIntExtra()
  • activity action
    • describe what act activity can do 
      • eg  . sending message
        • Intent.ACTION_SEND
  • implicit intent
    • Intent intent = new Intent (action);
    • need to have DEFAULT category
  • setType()
    • to describe intent dataType
  • Intent Resolution
    • compare intent content to intent filter in app AnroidManifest.xml
      • components name
      • action
      • data type
      • category
  • createChooser()
    • overwrite default Android activity  select window
      • add new topic
      • no default 
      • if no activity it will show mssage
  • getString(R.string.stringname) to get string resource value

No comments:

Post a Comment