1 / 21

Manifest File, Intents, and Multiple Activities

Manifest File, Intents, and Multiple Activities. Manifest File. Manifest file. AndroidManifest.xml required indicates application information activities (within application tag) Android SDK version activities used within the app services that will be used (Web, phone, etc.) other aspects.

della
Download Presentation

Manifest File, Intents, and Multiple Activities

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Manifest File, Intents, and Multiple Activities

  2. Manifest File

  3. Manifest file • AndroidManifest.xml • required • indicates application information • activities (within application tag) • Android SDK version • activities used within the app • services that will be used (Web, phone, etc.) • other aspects

  4. Manifest file – Application tag <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="edu.csci153.MultActivities" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="Screen2" android:label="Second Screen" > </activity> </application>

  5. Manifest File • Exploring the <application> tag • android:allowBackup=“true” • allows app and data to be backed up with a system restore • android:icon=“@drawable/ic_launcher” • Icon to display in the drawer • android:label="@string/app_name“ • Name of the icon in the drawer • <activity> • child of <application>

  6. Manifest File • Exploring the <activity> tag • android:name=“edu.csci153.MultActivities” • Associated .java file • android:label=“@string/app_name” • Text that appears in title bar when this activity is displayed • <intent-filter> • child of <activity>

  7. Manifest File • Exploring the <intent-filter> tag • <action android:name="android.intent.action.MAIN" /> • Indicates this is the main entry point of the application • <category android:name="android.intent.category.LAUNCHER" /> • Indicates that the activity should be launched • Without these lines, the application is started but no activity is presented • intent-filters ‘filter’ what an object can do – if there is no action defined within the filter, they implicitly deny that the action can be performed

  8. Intents

  9. Intent • Class within Android • android.content.Intent • contains information regarding some action to be performed • starting the phone dialer • starting an activity • opening a web page • other

  10. Intent Example • Starting the phone dialer (no special permission needed) Intentintent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:5551234")); startActivity(intent);

  11. Intent Example • Opening a web page • Permission in manifest file • well over 100 different permissions • access internet, bluetooth, vibrate phone, change wall paper, etc. • http://developer.android.com/reference/android/Manifest.permission.html • uses-permission tag (child of manifest tag) <uses-permission android:name="android.permission.INTERNET" /> • Intent in corresponding java file Intentintent = new Intent(Intent.ACTION_VIEW, Uri.parse(”http://www.google.com”)); startActivity(intent);

  12. Intent Example • Opening an Activity Intent intent = new Intent(this, Screen2.class); startActivity(intent); • ‘this’ refers to the current activity • Screen2.class refers to the class file associated with the new activity to be opened • implies a corresponding Sreen2.java file exists • activity MUST be referenced in the manifest file

  13. Intents • Methods in the Intent class • The intent class has many methods to put or retrieve data • useful when one Activity launches another • put methods used by current Activity that will instantiate another Activity • get methods used by new instantiated Activity

  14. Intents • put… methods • put… allows information to be passed from current Activity to newly instantiated Activity • putExtra – simple data types and arrays • passing an integer • i.putExtra(“Key1”, 17); • Key1 – name of the integer to be passed • 17 – contents of the integer to be passed • passing a String • i.putExtra(“Key2”, “Value”); • passing an array • i.putExtra(“Key3”, new int [] {1, 2, 3});

  15. Intents • get… methods • get… allows information to be retrieved by the newly instantiated Activity • get…Extra – datatype must be known • getting an integer • getIntent().getIntExtra(“Key1”, 0); • 2nd argument is default value in case Key1 does not exist, or is not an integer • getIntent().getStringExtra(“Key2”); • retrieves value associated with Key2 • if no value, null is returned – no default option • getting an array • int [] z = getIntent().getIntArrayExtra(”Key3"); • if no value, null is returned – no default option

  16. Activities • Helpful hints about Activities • Each Activity has: • a corresponding .java file • at least one corresponding .xml file (may have additional menu file) • Each Activity must be referenced in the manifest file • The Activity class has a getIntent() method to retrieve the intent that initiated it • Each activity has a lifecycle • http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

  17. Activity lifecycle methods • Most important (signatures) • protected void onCreate(Bundle savedInstanceState); • called when created or phone rotated • protected void onPause(); • called when still visible but focus is lost • protected void onResume(); • called when focus is set AFTER being completely obscured • When overriding methods, super class’ version MUST be called

  18. Example method @Override protected void onResume() { super.onResume(); //Code goes here //Clear fields, set focus, restart sensor //listeners, etc. }

  19. Additional information • Sending information back • When one Activity finishes, activity can be sent back to the Activity that started it as follows: • In the original Activity, 2 methods are needed • startActivityForResult(Intent data, intrequestCode); must be called to open Activity • any non-negative integer can be used for requestCode • onActivityResult(intrequestCode, intresultCode, Intent data) must be implemented • Called when other Activity exits – just before onResume() • requestCode is same code from above • resultCode is sent from closing Activity • data stores any data that was sent back

  20. Additional information • Continued from previous slide • In the opened Activity, 1 method is needed • setResult(intresultCode, Intent data) must be called prior to finish() • any integer can be used for resultCode • Activity.RESULT_CANCELED • Activity.RESULT_OK • other • data stores any data to be sent back

  21. Sample Code • In original Activity public void openActivityAndWaitForResults() { Intent i = new Intent(this, SecondActivity.class); i.putExtra("StringValue", "Coming to you"); startActivityForResult(i, 1); } protected void onActivityResult(intrequestCode, intresultCode, Intent data) { Toast.makeText(this, "From second activity: " + requestCode + " " + resultCode + " " +data.getStringExtra("InfoBack"), Toast.LENGTH_LONG).show(); } • In opened Activity public void closeActivityAndSendInfoBack () { Intent output = new Intent(); output.putExtra("InfoBack", "Back at you!"); setResult(7, output); finish(); }

More Related