Summary of Android interview basics (1)

Status: Incomplete

Summary of Android interview basics.

Activity

The life cycle

  • 启动Activity:onCreate->onStart->onResume
  • Lock the screen or be covered by other Activity: onPause->onStop
  • Unlock or return to the foreground from the overridden state: onRestart->onStart->onResume
  • Jump to other Activity or press Home to enter the background: onPause->onStop
  • Return to this Activity: onRestart->onStart->onResume
  • Exit this Activity: onPause->onStop->onDestory
  • The dialog popup will not execute any life cycle (Note: If the dialog box is an Activity (Theme is Dialog), the life cycle will still be executed)
  • Jump from A to B: A-onPause->B-(onCreate->onStart->onResume)-A-onStop
  • Return from B to A: B-onPause->A-(onRestart->onStart->onResume)-B-(onStop->onDestroy)

onWindowFocusChanged method

  • Called when the window focus changes
  • Gain focus after onResume, lose focus after onPause

onSaveInstanceState method

  • Used to save the state of the Activity to store some temporary data
  • Activity is overwritten or enters the background, it will be called by kill due to insufficient system resources
  • Called when the user changes the screen orientation
  • Jump to other Activity or press Home to enter the background will be called
  • Will be called before onStop, and the order of onPause is not fixed

onRestoreInstanceState (Bundle savedInstanceState) 方法

  • Used to restore the saved temporary data, the Bundle parameter of this method will also be passed to the onCreate method, you can also restore the data in the onCreate(Bundle savedInstanceState) method
  • It will be called after returning to this Activity after being killed due to insufficient system resources
  • Called when the user changes the screen orientation to rebuild the Activity
  • will be called after onStart

Question: When the memory is insufficient, how to maintain some states of the Activity, and in which method to perform specific operations?

Save the state of the Activity in the onSaveInstanceState method, and restore the state of the Activity in the onRestoreInstanceState or onCreate method

screen orientation

  • Vertical screen: Set android:screenOrientation="portrait" for the specified Activity in AndroidManifest.xml or call it in the onCreate methodsetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  • Landscape: Set android:screenOrientation="landscape" for the specified Activity in AndroidManifest.xml or call it in the onCreate methodsetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

By default, the Activity will be destroyed and rebuilt when the screen orientation is switched

  • onPause->onStop->onDestroy->onCreate->onStart->onResume

When configured as android:configChanges=”orientation|screenSize”

  • Only the onConfigurationChanged method will be called
  • Note that this property has no effect when the screenOrientation property is configured.

boot mode

  • standard standard mode (activity default): every time startActivity is called, the activity will be created.
  • singleTop single top mode: every time you call startActivity, you need to determine whether the current activity has been created and check whether the top of the task stack is the current activity. If so, call the onNewIntent method, if not, create a new activity instance.
    Application scenario: Illegal programmers, rogue programs written, keep popping up a certain page.
  • singleTask single task stack mode: If the current activity already exists in the task stack, and then call startActivity, the onNewIntent method of the current task stack will be called. At the same time, all the above activities will be cleared and popped out of the stack.
    Application scenario: If an interface displays The resources are very large, and the instance only needs to be initialized once.
  • singleInstance single instance mode: The activity will be instantiated in a new task stack, and other activities will not be created in the new task stack. It will always be initialized once in the whole system.
    Application scenarios: In the whole system, only A page that needs to be initialized once.

How can different activities of the same program be placed in different task stacks?

  • Different affinity properties need to be set for different activities, and the Intent that starts the activity needs to contain the FLAG_ACTIVITY_NEW_TASK flag.

How to set an Activity as the style of the window

  • Set the theme for the Activity to android:theme=”@android:style/Theme.Dialog”

How to quit Activity? How to safely exit an Application that has called multiple Activities?

  • Exit the Activity and finish directly.
  • Throwing an exception to force exit, this method makes the program Force Close by throwing an exception. Verification can be done, but the problem that needs to be solved is how to make the program end without popping up the Force Close window.
  • Record open Activity: Every time an Activity is opened, it is recorded. When you need to exit, just close each Activity.
  • Send a specific broadcast: When the application needs to be terminated, a specific broadcast is sent. After each Activity receives the broadcast, it can be closed.
  • Recursive exit: use startActivityForResult when opening a new Activity, then add the flag yourself, process it in onActivityResult, and close it recursively.

Fragment life cycle

onAttach->onCreate->onCreateView->onActivityCreated->onStart->onResume->onPause->onStop->onDestroyView->onDestroy->onDetach

Differences and connections between Broadcast, Content Provider and AIDL

All three of these can realize cross-process communication, so what is the difference between them in terms of efficiency, scope of application, security, etc.?

Broadcast: Used to send and receive broadcasts! Realize the sending and receiving of information!
AIDL: for mutual invocation of services between different programs! Implemented the function of one program serving another program!
Content Provider: used to artificially expose the program's database! Realize that a program can perform relative operations on the database of another program!

Broadcast, since it is a broadcast, its advantages are: applications that have registered this broadcast receiver can receive the broadcast, and the scope is wide.
The disadvantage is: the speed is slow, and the thing must be processed within a certain period of time (the execution of onReceive must be within a few seconds), otherwise the system will give an ANR.
AIDL is used for inter-process communication, similar to a protocol. The advantages are: fast speed (the bottom layer of the system is directly shared memory), stable performance, high efficiency, and it is generally used for inter-process communication.
Content Provider, because it only exposes its own database, other programs can come to obtain data, the data itself is not real-time, unlike the previous two, it just plays a data supply role. It is generally used by a mature application to expose its own data. If you are for inter-process communication, don't use this, this is not real-time data.

Reference link: http://blog.csdn.net/woaieillen/article/details/8185531

Handler mechanism

  • Andriod provides Handler and Looper to meet the communication between threads. Handler first in first out principle. The Looper class is used to manage the message exchange (MessageExchange) between objects within a specific thread.
  • Looper: A thread can generate a Looper object, which manages the MessageQueue (message queue) in this thread.
  • Handler: You can construct a Handler object to communicate with Looper, so as to push new messages to MessageQueue; or receive messages sent by Looper from Message Queue).
  • Message Queue (message queue): used to store messages placed by threads.
  • Thread: UIthread is usually the main thread, and Android will create a MessageQueue for it when it starts the program.

ListView freeze reason

  • ConvertView does not use setTag and getTag methods in Adapter's getView method;
  • In the getView method, the initialized assignment of ViewHolder or the display status of multiple controls and the display of the background are not optimized, or it contains complex calculations and time-consuming operations;
  • In the getView method, the row of inflate is nested too deeply (the layout is too complicated) or because there are large pictures or backgrounds in the layout;
  • Adapter redundant or unreasonable notifySetDataChanged;
  • The listview is nested in multiple layers, and multiple onMessures cause lag. If multiple layers of nesting cannot be avoided, it is recommended to set the height and width of the listview to fill_parent. If it is a listview inherited by code, please don’t forget to provide your Add LayoutPrams to the inherited class, note that both height and width are fill_parent;

Data storage, what are the ways of data persistence

  • SharedPreference
  • DatabaseSQLite
  • document
  • ContentProvider Content providers such as contacts
  • The internet

Process priority

  • foreground process
  • visible process
  • service process
  • backstage process
  • empty process

Difference between JVM and Dalvik virtual machine

  • JVM:
    .java -> javac -> .class -> jar -> .jar
    Architecture: Heap and stack architecture.
  • DVM:
    .java -> javac -> .class -> dx.bat -> .dex
    Architecture: Register (a cache on the cpu)

The difference and connection of px,sp,dip,dp in Android, convert 60px to dp of the corresponding screen, and fill in the following table

ldpi mdpi tvdpi hdpi xhdpi xxhdpi xxxhdpi
80 60 45 40 30 20 15

px = dip * density / 160

ldpi = 60px/0.75 = 80dp
mdpi = 60px/1 = 60dp
hdpi = 60px/1.5 = 40dp
xhdpi = 60px/2.0 = 30dp
xxhdpi = 60px/3.0 = 20dp
xxxhdpi = 60px/4.0 = 15dp

PPI = Pixels per inch, the number of pixels per inch, that is, "pixel density"
xxxhdpi: 4.0
xxhdpi: 3.0 (1080 1920) ppi=480
xhdpi: 2.0 (720
1280) ppi=320
hdpi: 1.5 (480 800) ppi= 240
mdpi: 1.0 (baseline)(320
480) ppi=160
ldpi: 0.75 (240*320) ppi=120

tvdpi:ppi=213

dip: device independent pixels. Different devices have different display effects. This is related to the device hardware. Generally, we recommend using this to support WVGA, HVGA and QVGA, which does not depend on pixels.
dp: dip is the same as
px: pixels (pixels).
pt: point, is a standard length unit, 1pt=1/72 inch, used in the printing industry, very easy to use;
sp: scaled pixels (magnified pixels). Mainly used for font display best for textsize.
in (inches): unit of length.
mm (millimeters): unit of length.

According to px = dip * density / 160, when the screen density is 160, px = dip
According to google's suggestion, the font size of TextView is best to use sp as the unit, and viewing the source code of TextView shows that Android uses sp as the font size unit by default. Use dip as the unit for other elements.

Reference link: http://www.jb51.net/article/38506.htm
http://www.cnblogs.com/bluestorm/p/3640786.html

Briefly describe the composition of an Android application

  • Presentation layer for Activites applications.
    Every interface of the application will be an extension of the Activity class. Acitvities uses a view (View) to form a GUI to display information and respond to user actions. As far as desktop development is concerned, an activity (Activity) is equivalent to a form (Form).
  • Invisible workers in Services applications.
    The Service component runs in the background, updates your data source and visible Activities, and triggers Notifications. Used to perform long-term processing that still needs to continue when the application's Activities are not active or visible.
  • Content Providers Shareable data store.
    Content Providers are used to manage and share application databases. Is the preferred way of data sharing across application boundaries.
  • Intents An inter-application messaging framework.
    Using Intents you can broadcast messages system-wide or send messages to a target Activity or Service to indicate that you want to perform an action.
  • Widgets are visual application components that can be added to the home screen.
    As a special variant of Broadcase Receiver, widgets allow you to create dynamic, interactive application components for users that can be embedded into the home screen interface.

What are the animations in Android and what are the differences?
There are two types of animations, one is Tween animation and the other is Frame animation. Tween animation, this implementation can make the view component move, zoom in, zoom out and change the transparency; another frame animation, the traditional animation method, is realized by playing arranged pictures in sequence, similar to a movie.

Briefly describe the principle of silent installation, how to achieve silent installation without root privileges?

  • Disguised as a system application, it is necessary to sign the app with the signature of the system application, but these signatures are useless on mobile phones such as Xiaomi.
  • It can also be achieved by placing the application in the system/app directory.

If the classes in the com.google.gson package are not allowed to be confused, and all the beans in the project are not allowed to be confused, but since the project is developed by multiple people, the package where the bean is located is not sure, and only the beans are serialized, please Manually configure obfuscation rules for a project

1
2
-keep class com.google.gson.** { *; }
-keep class * implements java.io.Serializable {*;}

Manually write a gradle script and configure the so library and libs\gson-2.5.jar in the libs\arm64-v8a directory into the project dependencies
Specify the JNI folder as libs

1
2
3
4
5
sourceSets{
       
       
        main{
       
       
            jniLibs.srcDir(['libs'])
        }
    }

add gson dependency

1
compile files('libs/gson-2.5.jar')

Android event delivery and processing mechanism

  • dispatchTouchEvent() event dispatch
  • onInterceptTouchEvent() event interception
  • onTouchEvent()

First, the parent control receives the event for distribution, calls interception, returns true for consumption and calls onTouchEvent, and returns false to pass the event to the child control. Returns true for consumption, returns false, returns the event, the onTouchEvent of the parent control returns true for consumption, returns false and the event is lost
. View without subclasses does not have an onInterceptTouchEvent() event interception method

How to update UI in child thread

  • The first: Handler+Message
  • The second:

    1
    2
    3
    4
    5
    6
    
    new Handler(context.getMainLooper()).post(
    new Runnable(){
             publicvoidrun(){
             //更新UI    }  });  
             
          
             
        
    
    
    
  • the third

    1
    2
    3
    4
    5
    6
    7
    
    ((Activity)context)runOnUiThread(
    new Runnable(){
                  public void run(){
                    //更新UI    }  }  );  
              
    
              
    
    
    
    
    

What is the difference between postInvalidate and invalidate?

  • are used to refresh the interface
  • postInvalidate() is used in child threads
  • invalidate() is used on the main thread

The difference between notifyDataSetChanged and notifyDataSetInvalidated

  • notifyDataSetInvalidated(), will redraw the entire control (restore to the initial state)
  • notifyDataSetChanged(), redraw the current visible area

Please introduce the five commonly used layouts in Android

  • FrameLayout
  • LinearLayout (linear layout)
  • AbsoluteLayout (absolute layout)
  • RelativeLayout (relative layout)
  • TableLayout

Difference between Serializable and Parcelable

  • Can be serialized and can be used for data transfer between Intent
  • Serializable is a serialization interface in Java. It is simple to use but expensive. The serialization and deserialization process requires a lot of I/O operations.
  • Parcelable is more suitable for the Android platform. It is troublesome but efficient to use, and is mainly used for memory serialization.

Which is more efficient, file or database

  • It is efficient to use the database when the amount of data is large
  • Use files efficiently when the amount of data is small

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324160436&siteId=291194637