Presentation is loading. Please wait.

Presentation is loading. Please wait.

Manifest File, Intents, and Multiple Activities. Manifest File.

Similar presentations


Presentation on theme: "Manifest File, Intents, and Multiple Activities. Manifest File."— 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" > <activity android:name="Screen2" android:label="Second Screen" >

5 Manifest File Exploring the 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 – child of

6 Manifest File Exploring the 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 – child of

7 Manifest File Exploring the tag – Indicates this is the main entry point of the application – 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) Intent intent = 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) – Intent in corresponding java file Intent intent = 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); » 2 nd 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, int requestCode); must be called to open Activity » any non-negative integer can be used for requestCode – onActivityResult(int requestCode, int resultCode, 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(int resultCode, 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(int requestCode, int resultCode, 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(); }


Download ppt "Manifest File, Intents, and Multiple Activities. Manifest File."

Similar presentations


Ads by Google