Android App Development-page jump and data transfer (re-Seihan)

Source: Internet
Author: User

Android Four components:Activity,Service,broadcast Receiver,Content Provider

Create activity

  • Defining Java classes, inheriting activity classes

  • Configure activity tags in the manifest file

    • The Activity tab will create a shortcut icon in the system if it comes with this part of the code

      < Intent-filter >    <  android:name= "Android.intent.action.MAIN"/>    <   android:name= "Android.intent.category.LAUNCHER"/></ Intent-filter >

    • An application can create multiple shortcut icons on the desktop.

    • Activity name, icon can be different from application name, icon

      android:icon= "@drawable/ic_launcher" android:label= "@string/app_name"

Activity's Jump

The activity's jump needs to create the intent object, specifying the intent object's parameters to jump to the activity

To implement a jump by specifying activity, called an explicit intent

To implement a jump by specifying an action, called an implicit intent

An explicit jump
    • Jump to activity in the same app

      New Intent (); Intent.setclass (this, secondactivity. class); // specifies the current context and the byte code of the target activity startactivity (intent);

    • Jump to activity in a different app

      New Intent (); // Specifies the package name of the app where the target activity resides and the package name of the target activity plus the class name, which launches the system's own dialer app intent.setclassname ("Com.android.dialer", " Com.android.dialer.DialtactsActivity "); StartActivity (intent) ;

Implicit jump
    • Implicit intent to jump to call activity

      New Intent (); intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("tel:10086")); StartActivity (intent);

    • To allow an activity to be implicitly launched, you need to configure the Intent-filter sub-label under the Activity tab under the app manifest file

    • The system looks for a intent-filterthat matches the implicit intent we created in all of the app's manifest files, finds it, launches it, throws an exception if it's not found.

    • Matching is what attributes are defined in Intent-filter, and what attributes must be set in the intent we create

    • Under the Activity tab of the manifest file, configure the Intent-filter sub-tab, where you can configure the action and category

Application Scenarios
    • start an activity in the same app with an explicit intent. Explicit startup efficiency is significantly higher than implicit because the display directly specifies the target activity, which implicitly needs to traverse all the app's manifest files

    • activate activity in different applications with implicit intent. When using implicit intent to jump to activity in another app, if the system finds more than one intent-filter matches the intent we created, a dialog box pops up, listing all the matching activity to let the user choose. For example, if we have a number of browser apps installed on our phone, when we click on a URL, a dialog box will pop up to let us choose which browser to use to open the page, which is because it is an implicit intent to jump to the browser activity. Some applications need to receive data when the activity starts, such as calling the activity to receive a phone number, or only using implicit intent

Data transfer when activity jumps

    • When the activity jumps, the data can be encapsulated in the intent.

      New Intent (This, secondactivity.  Class); Intent.putextra ("name", name), Intent.putextra ("Age", age); StartActivity (Intent );

    • Data types that can be encapsulated in intent: eight basic data types and strings, their arrays, objects that implement the serialization interface, and bundle objects. Data can also be encapsulated into bundles, and bundles into intent

    • Remove data from the target activity that jumps to

      Intent Intent = Getintent ();    // gets the intent that initiated the activity String name = Intent.getstringextra ("name"); int age = Intent.getintextra ("Age");

Life cycle of activity

    • onCreate (): callback When creating activity

    • OnStart (): Visible on the screen, but not yet focusable

    • Onresume (): Visible and get focus

    • onPause (): visible, but loses focus

    • onStop (): not visible

    • OnDestroy (): called when destroying

    • Onrestart (): Called when activity is never visible to visible

    • Press the back key, the system callback OnPause (),onStop (),OnDestroy (), the activity is destroyed, the process is still in, and if you enter the activity again, the system will callback onCreate (),OnStart (),onresume (), activity re-entered the running state

      • By pressing the back key, the Android system will callback the onbackpressed () method, which calls the finish (), so the activity is destroyed
    • Press Home key, the system callback OnPause (),onStop (), the activity enters the stop state, the process is still in; If you enter the activity again, the system will callback in turn Onrestart (),OnStart (),onresume (), activity re-entered the running state

    • When the phone is out of memory, it kills the process that was started before, and it locks down which process is killed according to the LRU (least recently used) algorithm

Four startup modes for activity

Each app has an activity task stack that stores the activity that was started. The activity's startup mode determines the operation rules of the task stack.

    • Standard: The default is the normal mode, each time through this mode to start the target activity, Android will always create a new instance for the target activity, and add the activity instance to the task stack

    • singletop: task stack top singleton mode, only a little different from standard mode: When the target activity to start is already at the top of the task stack, the system does not recreate the instance of the target activity, but instead directly re-uses the existing activity instance

      • Application: Browser Bookmark History Interface Combinedbookmarkhistoryactivity
      • Application: SMS Session Recording Interface Conversationlist
    • singletask: In the task stack singleton mode, activity with this startup mode can have only one instance within the same task stack. If the activity is not created for any instance, the merge stack is created when it is started, and if an instance has already been created, it is returned (retired) to the activity when it is started.

      • Application: Browser Browsing Interface browseractivity
    • singleinstance: Global singleton mode, activity with this startup mode can have only one instance of the entire Android system memory. If the target activity that is to be started does not exist, the system creates a completely new task stack, creates an instance of the target activity, and pushes him to the top of the new task stack, if the target activity that will be started already exists, regardless of which application it is in, In which task stack, the system transfers the task stack where the activity is located to the foreground, so that activity can be displayed. It should be noted that activity initiated in singleinstance mode is always at the top of the task stack, and only that activity in the task stack

      • Application: Call Interface Incallscreen

Toggle between the activity and the screen

  • By default, the toggle will cause the activity to destroy the rebuild and trigger the life cycle method to re-execute

  • In some special applications, such as the game, do not want to switch between the screen and the activity is destroyed rebuild, if not save will lose the game data

      • Use the following code in the manifest file to not recreate the activity when switching between the two screens

        Android:configchanges= "Orientation|screensize|keyboardhidden"

      • The following code can be used in the manifest file to write the screen orientation of the dead activity

        android:screenorientation= "Portrait"     <!--  vertical screen --android: screenorientation= "Landscape"    <!--  horizontal screen  --

      • The following code can be used in the Java file to write the screen orientation of the activity (overwriting the settings in the manifest file)

        Setrequestedorientation (activityinfo.screen_orientation_portrait);  // Vertical Screen // Horizontal Screen

Return data when activity is destroyed

Steps:
  1. Specify the request code Requestcode start a new activity

    Startactivityforresult (Intent, Requestcode);

  2. Set the data to be returned in the new activity

    New  Intent ();d Ata.putextra("Tel", tel); Setresult (ResultCode, data);  //  Specify a result code ResultCode and return the data to the original activityfinish ();

  3. Implement the method in the original activity

    // gets the returned data via data onactivityresult (intint  resultcode, Intent data) {}

  4. Determine the function of the return value data by judging the request code and the result code

    • The data returned by the request code Requestcode is determined from which activity

    • The result code returned by the activity ResultCode determine which data is returned by the activity in data

Android App Development-page jump and data transfer (re-Seihan)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.