Android face test summary Android Basics (i)

Source: Internet
Author: User

Android face test summary Android Basics (i)

A long time to graduate, write a program also long, from high school has been written to now for many years, ah, learning a lot of things, before the information and knowledge points to share slowly. Here the main summary of Android may be out of the interview topic more comprehensive \ Detailed, persistent update, also welcome to add, correct, criticize.

Android Four Components Introduction
  1. Please briefly describe the four components of Android.
    Answer: The Android system has four components, these four components constitute the framework of Android application, and then by the intent contact these four kinds of components.

    • Activity is used to display Android's program interface, an application often has multiple interfaces, so there will be multiple activity in an application.
    • The service does not have an interface behind the scenes and will always run in the background. Often used to do data processing, but also to do some scheduled tasks.
    • Broadcast receiver is an Android radio receiver that acts as a broadcast recipient in the broadcast mechanism, and Android is filled with various broadcasts, all of which need to selectively receive some useful broadcasts and then process those broadcasts.
    • Content provider can be literally translated into the contents provider, it is used to share data between different applications, can be used to provide an application of data to other applications.
  2. What's the difference between activity, Intent, Content Provider, and service in Android?
    Answer:

    • Activity: Active, is the most basic Android application component. An activity is a separate screen, and each activity is implemented as a separate class and inherits from the active base class.
    • Intent: Intention to describe what the application wants to do. The most important part is the data corresponding to the action and the action.
    • Content Provider: An android application can save their data to a file, a SQLite database, or even any valid device. Content providers can work when you want to share your app data with other apps.
    • Service: A program that has a long life cycle and no user interface.
  3. What information is included primarily in the Manifest.xml file?
    Answer: Manifest: Root node, which describes all the contents of the package.
    • Uses-permission: Ask for the security license you need to give your package a normal operation.
    • Permission: A security license is declared to limit which programs can be components and features in your package.
    • Instrumentation: Declares the code used to test this package or other Packages directive component.
    • Application: Contains the root node of the application level component declaration in the package.
    • Activity:activity is the primary tool for interacting with users.
    • Receiver:intentreceiver enables the application to obtain data changes or actions that occur even if it is not currently running.
    • Service:service is a component that can run at any time in the background.
    • Provider:contentprovider is a component used to manage persisted data and publish it to other applications.
Activity
  1. What is Activity?
    One of the four components, one user interface corresponds to an activity Setcontentview (),//The layout to be displayed button.setonclicklinstener{
    The activity is a subclass of the Context and implements both Window.callback and Keyevent.callback to handle events that interact with the form user. I develop commonly used to have
    Fragmentactivitiylistactivity, preferenceactivity, tabacitivty, etc...
  2. Activity life cycle
    Activity from creation to destruction has multiple states, from one state to another, firing the appropriate callback methods, including: OnCreate onStart onresume onPause onStop OnDestroy

    In fact, these methods are 22 corresponding, onCreate creation and OnDestroy destruction;
    OnStart Visible and OnStop not visible, onresume editable (i.e. focus) and onPause;
    If the interface has a common feature or function, it will define itself as a baseactivity. Display and destruction of the progress dialog box
    OnCreate the initialization work when the Activity is created, such as setting the Contentview of the page, receiving parameters, etc.

      • Onrestart calls this parameter when it restarts after the activity's OnStop

      • OnStart is starting, is about to start, does not appear in the foreground, and cannot interact with the user. It can be understood that the initialization is complete, but we can't see it in the background. The

      • Onresume is visible and in the active state, in the foreground. The biggest difference between

        and OnStart is that OnStart is already initialized in the background, but cannot be interacted with.

      • OnPause lose focus can not be interactive, in the background.

      • OnStop is about to stop and do some sort of heavy-duty recycling.

      • Ondestory activity is about to be destroyed, and you need to do some recycling and resource release classes.

        reminder: from activity A, open another activity b,b this activity becomes visible only after the onpause of a is executed, so in order not to affect the display of B, , you can consider putting these operations into OnStop, when B is visible, (meaning a->onpause after B->oncreate,onstart, Onresume #转问题4)

  3. How do I save the status of my Activity?
    The status of the activity is usually saved automatically and is only needed if we need to save additional data.
    in general, the activity instance after calling the OnPause () and OnStop () methods still exists in memory, and all information and state data for the activity will not disappear, and all changes will be preserved when the activity returns to the foreground.
    However, when the system is out of memory, the activity after calling the OnPause () and OnStop () methods may be destroyed by the system, where the activity's instance object is not present in memory. If this activity goes back to the foreground, the changes made will disappear. To avoid this situation, we can override the Onsaveinstancestate () method. The
    Onsaveinstancestate () method accepts a bundle type parameter, and the developer can store the state data in the bundle object, so that if the activity is destroyed by the system, when the user restarts the activity and calls its The
    OnCreate () method, the bundle object described above is passed as an argument to the OnCreate () method, and the developer can remove the saved data from the bundle object and then use that data to restore the activity to the state before it was destroyed.
    It is important to note that the Onsaveinstancestate () method is not necessarily called because some scenarios do not need to save state data. For example, when the user presses the back key to exit activity, the user obviously wants to close the activity, and there is no It is necessary to save the data for the next recovery, i.e. the Onsaveinstancestate () method will not be called.
    If the Onsaveinstancestate () method is called, the call occurs before the OnPause () or OnStop () method.

    @OverrideprotectedvoidonSaveInstanceState// TODO Auto-generated method stub super.onSaveInstanceState(outState);}
  4. What are some of the methods that must be performed when jumping between two Activity?
    In general, for example, there are two activity, called a A, a, when the B component is activated in a, a calls the OnPause () method, then B calls OnCreate (), OnStart (), Onresume ().
    This time B overrides the form, and A calls the OnStop () method. If B is a transparent, or a dialog box style, the OnStop () method of a is not called.
  5. Life cycle of Activity when switching between portrait and screen
    The life cycle at this time is related to the configuration in the manifest file.
    1. When you do not set the activity's android:configchanges, the screen will re-invoke each life cycle by default destroying the current Activity first and then reloading. 2. Set up Activity
    Android:configchanges= "Orientation|keyboardhidden|screensize", the screen does not recall the individual lifecycles, only the Onconfigurationchanged method is executed.
    Usually in the game development, the screen orientation is written dead.
  6. How to set an activity as a window style only needs to configure the following properties for our activity.
```7. 如何退出 Activity?如何安全退出已调用多个 Activity 的 Application?     1. 通常情况用户退出一个 Activity 只需按返回键,我们写代码想退出 activity 直接调用 finish()方法就行。  2. 记录打开的 Activity:每打开一个 Activity,就记录下来。在需要退出时,关闭每一个 Activity 即可。//伪代码```List<Activity> lists ;// 在 application 全局的变量里面 lists = new ArrayList<Activity>();lists.add(this);for(Activity activity: lists){           activity.finish();        }lists.remove(thisclass="se-preview-section-delimiter"></div>
  1. To send a specific broadcast:
    When you need to end the app, send a specific broadcast, and after each Activity receives the broadcast, it's off.
    //给某个 activity 注册接受接受广播的意图 registerReceiver(receiver, filter)
    //如果过接受到的是 关闭 activity 的广播 activity finish()掉
  2. Recursive exit
    Call the Finish () method to put the current
    Use Startactivityforresult when opening new Activity, then flag it yourself, process it in Onactivityresult, and turn it off recursively.
    5, in fact, can also be achieved through the intent flag to achieve intent.setflags (intent.flag_activity_clear_top) activation of a new ACTIVITY. At this point, if the activity is already in the task stack, the system will kill all the activity on the activity. In fact, the equivalent to the Activity configuration of the startup mode is Singletop.

      1. What is the difference between the four startup modes of Activity, Singletop and singletask? The usage pattern for general bookmarks is singletop, so why not use Singletask?

    Singletop is similar to standard mode. The only difference is that when a jump object is an activity at the top of the stack (which should be understood as the activity seen by the user), the program will not generate a new activity instance, but instead jump directly to the activity instance that is currently on top of the stack. Take the above example, when the Act1 is Singletop mode, the execution of the jump after the stack is still only

    An instance, if you press the return key now the program will exit directly.
    Both the Singletask mode and the SingleInstance mode are created with only one instance. In this mode
    , the program does not generate a new instance, regardless of whether the object being jumped is on top of the stack (as long as the instance is already in the stack). This mode is quite useful, in the future of multi-activity development, often because of the relationship between the jump to generate multiple instances of the same page, this is always a bit bad in the user experience, and if you declare the corresponding activity as Singletask mode, this problem will not exist. The Activity on the homepage is very common

      1. What is the difference between the context in Android and activity,appliction?
        Same: Both Activity and application are sub-classes of the Context.
        Context is literally understood to be the meaning of contexts, and in practice it is indeed the total use of various parameters and variables in the context of management, so that we can easily access the various resources.
        Different: The maintenance life cycle is different. The Context maintains the life cycle of the current Activity, and application maintains the life cycle of the entire project. When using context, be careful about memory leaks, prevent memory leaks, and pay attention to several things:
      2. Do not allow objects with long life cycles to refer to the activity context, which guarantees that the object referencing the activity is the same as the activity itself life cycle.
      3. For objects that have a long life cycle, you can use Application,context.
      4. Avoid non-static inner classes, use static classes as much as possible, avoid life cycle problems, and note the life cycle changes that internal classes cause to external object references.

      5. What is the Context?
        1, it describes the information of an application environment, that is, the context.
        2. The class is an abstract class, and Android provides a concrete implementation class (CONTEXTIML) for that abstract class.
        3, through it we can get the resources and classes of the application, also include some application-level operations, such as: Start an Activity, send broadcasts, accept Intent, information, etc.

    Attach a context inheritance diagram

Intent detailed
  1. use of Android intent
    What are the three main components--activity, service, and broadcast receiver activated?
    Answer: Intent is a runtime-bound message mechanism, and the three components--activity, service, and broadcast receiver are all activated by the message, which is intent.
    A intent object consists of six attributes, please list these six attributes each.
    Answer: The component name (Component name), action, data, category, additional information (Extra), and flags (flags).

    Answer: In an Android application, it is mainly composed of some components (activity,service,contentprovider,etc.) In the communication between these components, the intent assists the completion.
    As some people on the internet said, intent is responsible for the operation of the application in the action, the action involved in data, additional data description, Android according to the description of this intent, is responsible for finding the corresponding component, the intent passed to the calling component, and complete the call of the component. Intent is where the decoupling between the caller and the callee is implemented.
    In intent delivery process, to find the target consumer (another activity,intentreceiver or service), that is, the intent responder, there are two ways to match:

    1, display matching (Explicit): 
    public testb extents activity  
    {  
     .........  
    };  
     public class Test extends  activity  
    {  
         ......  
          public void switchactivity ()   
         {   
                Intent i =  New intent (test.this, testb.class);  
                 this.startactivity (i);  
         }  
    }  
     
    The code is concise, executes the switchactivity () function, and immediately jumps to the activity named Testb.  

    2. Implicit matching (implicit):

    Implicit matching, first to match several values of intent: Action, Category, data/type,component
    If you fill in the componet is the Test.class in the example above) this forms a display match. So this section only tells the first few matches. The matching rule is the maximum matching rule,

    1. If you fill in the action, If there is an activity in the Intentfilter segment of a program that contains the same action as defined in the manifest.xml, then this intent matches the target action, and if no type is defined in the filter segment, Category, then the activity is matched. But if you have more than two programs in your phone, a dialog box will pop up to show you the message.
    The value of action has a lot of pre-defined in Android, and if you want to go directly to your own defined intent receiver, you can add a custom action value to the recipient's Intentfilter (and set the category value to " Android.intent.category.DEFAULT "), set the action in your intent to intent, and you can jump directly to your own intent receiver. Because this action is unique in the system.
    2, Data/type, you can use the URI as data, such as URI uri = Uri.parse (http://www.google.com);
    Intent i = new Intent (Intent.action_view,uri); During the Intent distribution of the cell phone, the data type is judged according to the http://www.google.com scheme.
    The brower of the handset can match it, in the intenfilter of Brower Manifest.xml first have Action_view Action, also can handle http: the type,

    3, as for classification category, generally do not go in the intent set it, if you write intent receiver, in manifest.xml activity Intentfilter contains Android.category.DEFAULT So that all Intent that do not set the category (Intent.addcategory (String c);) will match this category.

    4, Extras (additional information), is a collection of all other additional information. You can use extras to provide extended information for your component, such as, if you want to perform the "Send e-mail" action, you can save the e-mail message's title, body, and so on in extras, to the e-mail sending component.

  2. Intentfilter (Intent Filter)
    Why introduce Intentfilter?
    Answer: For an explicit intent, its recipient has been specified, so the system will automatically send this intent to the specified component. However, for implicit intent, because the component name attribute is not specified, the system does not know which component name to send it to, so the system directly sends it out, so that all components have the right to receive, which requires defining which intent a component can receive. So we introduced the Intentfilter (intent filter).

Android face test summary Android Basics (i)

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.