Download presentation
Presentation is loading. Please wait.
Published byBrendan Boone Modified over 9 years ago
1
Activities and Intents Richard S. Stansbury 2015
2
What is an Activity? Activities are user visible application elements of an Android application Defined by: – Layout in XML – Logic in Java – Manifest defines its presence and ties activity to its Java class Note: layout can be defined in Java, but often as much of this is done in the XML as possible Started by: launcher, if main activity; an intent, otherwise
3
What is a Service? Services are like activities, but without a user interface Necessary for background activities – e.g. logging data in the background, listening to a socket, etc. Defined by: – Java class – Manifest informs the application that the service exists Started by: an intent
4
Activity Life Cycle
5
Saving Activity State @Override public void onSaveInstanceState(Bundle state) { super.onSaveInstanceState(state); state.putLong("chronBase", chronBase); state.putBoolean("chronActive", active); } From onCreate(.Bundle savedInstanceState): if (savedInstanceState != null) { active = savedInstanceState.getBoolean("chronActive"); if (active) { chronBase = savedInstanceState.getLong("chronBase"); startChron(); startTimer(); currentFlightID = db.getCurrentFlightID(); }
6
Intents Used to launch an activity, service, etc. Explicit intent: – Call an explicit activity from another activity Implicit intent: – Call an activity type from another activity – Define desired action and the system will choose or prompt the user to select the appropriate activity to perform task e.g. ACTION_SEND will prompt Gmail, twitter, etc. because they can send – Similar to web development with actions upon URIs
7
Explicit Intent Example /** * Creates a listener for the export button. */ View.OnClickListener exportListener = new View.OnClickListener() { //On click of stop button, if the logger is active, then stop it. public void onClick(View v) { loadExportActivity(); } }; public void loadExportActivity() { Intent intent = new Intent(this, ExportListActivity.class); startActivity(intent); }
8
Implicit Intent Example //Send File Uri uri = Uri.parse("file://" + outputFile.getAbsolutePath()); //Create the intent Intent i = new Intent(Intent.ACTION_SEND); i.putExtra(Intent.EXTRA_SUBJECT, "Exporting Flight Data"); i.putExtra(Intent.EXTRA_TEXT, "Please see attachment."); i.putExtra(Intent.EXTRA_STREAM, uri); i.setType("text/plain"); startActivity(Intent.createChooser(i, "Export Flight Data in CSV format..."));
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.