English: ## 4.1.1 Activity: Novice Practice
Category Android Basic Tutorial
Introduction to This Section:
This section begins to explain one of the four major components of Android, the Activity (activity). Let's first look at the official introduction to Activity: PS: Official website documentation: Activity
Introduction as Follows: General Idea:
>
An Activity is a component of an application that provides an area on the screen, allowing users to perform some interactive operations on it, such as making phone calls, taking photos, sending emails, or displaying a map! An Activity can be understood as a window that draws the user interface, and this window can fill the entire screen, or it may be smaller than the screen or float above other windows!
From the above passage, we can obtain the following information:
>
1. Activity is used to display the user interface, and users interact through Activity to complete related operations. 2. An App can have multiple Activities.
Well, the introduction of the general preface is here, if you want to understand in depth, you can continue to see the API, start the content of this section~
1. The Concept of Activity and the Lifecycle Diagram of Activity:
Attention:
>
1. The premise for calling onPause() and onStop() is: A new Activity has been opened! The former is the state where the old Activity is still visible; the latter is the state where the old Activity is no longer visible!
2. The Difference between Activity/ActionBarActivity/AppCompatActivity:
>
Before starting to explain the creation of Activity, let's talk about the difference between these three: Activity doesn't need to be said, the latter two were proposed for backward compatibility, they are all under the v7 package, ActionBarActivity has been deprecated, as the name implies, ActionBar~, and after 5.0, it was abandoned by Google, now using ToolBar... And now when we create an Activity in Android Studio, it defaults to inheriting: AppCompatActivity! Of course, you can also just write Activity, but AppCompatActivity provides us with some new things! Choose one of the two, Just as you like~
3. The Creation Process of Activity
PS:
>
Well, it has been said above that you can inherit from Activity and AppCompatActivity, but the latter provides some new things! In addition, remember, as long as you define the four major components in Android, whether you use them or not, you must declare this component in the AndroidManifest.xml, Otherwise, the program will exit directly during runtime and report a ClassNotFindException...
4. The Difference between onCreate() with One Parameter and Two Parameters:
I believe friends who use as will find that there are two parameters when overriding the onCreate() method of Act:
But normally there is only one parameter:
Well, this is the new method provided by 5.0, to use it, first set a property for our Activity in the configuration file:
android:persistableMode="persistAcrossReboots"
Then our Activity has the ability to persist, and we usually use it with two other methods:
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState)
I believe some friends are not unfamiliar with these two method names, the former will be called in the following situations:
>
Press the home button to return to the home page or press and hold to choose to run other programs.
Press the power button to turn off the screen.
Start a new Activity.
During the horizontal and vertical screen switch, it will definitely be executed, because during the horizontal and vertical screen switch, the Act will be destroyed first, and then recreated. Important principle: When the system destroys your activity "without your permission", onSaveInstanceState will be called by the system, This is the responsibility of the system, because it must provide an opportunity for you to save your data (you can save or not save).
And the latter method, like onCreate, can also retrieve the saved data from the former: It is usually executed between onStart() and onResume()! There are two methods that can retrieve the saved data to avoid Act jumping without closing, Then it does not go through the onCreate() method, but you want to retrieve the saved data~
Speaking of it: Speaking of this Activity having the ability to persist, the added PersistableBundle parameter makes these methods Have the ability to restore data after the system reboots!! And it does not affect our other serialization operations, wow, I'm not sure how to implement it for the time being, maybe it's saved in another file~! This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation.
English: intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setType("image/png"); startActivity(intent);
//4. Open the browser: // Open the Google homepage Uri uri = Uri.parse("http://www.baidu.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
//5. Send an email: (No chance for those who have castrated Google services!!!!) // Send an email to [email protected] Uri uri = Uri.parse("mailto:[email protected]"); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); startActivity(intent); // Send an email to [email protected] with the content "Hello" Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]"); intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); intent.putExtra(Intent.EXTRA_TEXT, "Hello"); intent.setType("text/plain"); startActivity(intent); // Send an email to multiple people Intent intent = new Intent(Intent.ACTION_SEND); String[] tos = {"[email protected]", "[email protected]"}; // recipients String[] ccs = {"[email protected]", "[email protected]"}; // carbon copy String[] bccs = {"[email protected]", "[email protected]"}; // blind carbon copy intent.putExtra(Intent.EXTRA_EMAIL, tos); intent.putExtra(Intent.EXTRA_CC, ccs); intent.putExtra(Intent.EXTRA_BCC, bccs); intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); intent.putExtra(Intent.EXTRA_TEXT, "Hello"); intent.setType("message/rfc822"); startActivity(intent);
//6. Display a map: // Open the location of Google Maps China Beijing (39.9°N, 116.3°E) Uri uri = Uri.parse("geo:39.9,116.3"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
//7. Route planning // Route planning: from a place in Beijing (39.9°N, 116.3°E) to a place in Shanghai (31.2°N, 121.4°E) Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
//8. Multimedia playback: Intent intent = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse("file:///sdcard/foo.mp3"); intent.setDataAndType(uri, "audio/mp3"); startActivity(intent);
// Get all audio files under the SD card, and then play the first one =-= Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
//9. Open the camera to take a photo: // Open the photo-taking program Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 0); // Retrieve photo data Bundle extras = intent.getExtras(); Bitmap bitmap = (Bitmap) extras.get("data");
// Another way: // Call the system camera application and store the captured photo Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); time = Calendar.getInstance().getTimeInMillis(); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment .getExternalStorageDirectory().getAbsolutePath()+"/tucue", time + ".jpg"))); startActivityForResult(intent, ACTIVITY_GET_CAMERA_IMAGE);
//10. Get and crop the picture // Get and crop the picture Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.putExtra("crop", "true"); // Enable cropping intent.putExtra("aspectX", 1); // Aspect ratio for cropping is 1:2 intent.putExtra("aspectY", 2); intent.putExtra("outputX", 20); // Width and height to save the picture intent.putExtra("outputY", 40); intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // Save path intent.putExtra("outputFormat", "JPEG");// Return format startActivityForResult(intent, 0); // Crop a specific picture Intent intent = new Intent("com.android.camera.action.CROP"); intent.setClassName("com.android.camera", "com.android.camera.CropImage"); intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp"))); intent.putExtra("outputX", 1); // Aspect ratio for cropping is 1:2 intent.putExtra("outputY", 2); intent
1.8 Project Related Analysis (Various Files, Resource Access)
2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
[2.5.8
6.2 Data Storage and Access - SharedPreferences to Save User Preferences
6.3.1 Data Storage and Access - An Introduction to SQLite Database
7.1.1 Android Network Programming Essentials and Http Protocol Learning
8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (I)
8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (II)
8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (III)
8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (IV)
8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (V)
[8.3.12 Paint API - PathEffect (Path Effect)
12.5 DrySister Viewing Girls App (First Edition) - 5. Code Review, Adjustment, and Log Class Writing