Android Engineer Face Questions Encyclopedia _android

Source: Internet
Author: User
Tags inheritance static class

The following is the time to meet the interview problems to organize, to share with you for your reference, the specific content as follows

Java Basics:

1, the reason for the memory leakage

1), resource object not closed.
such as cursor, file and other resources. They will be closed in finalize, but this is too inefficient. Easily lead to memory leaks.
Sqlitecursor, when the volume of data is very easy to leak
2), when using adapter, there is no converview using the system cache.
3), Instant call Recycle () release the bitmap that is no longer in use.
Reduce bitmap sampling rate appropriately, such as:

Bitmapfactory.options Options = Newbitmapfactory.options ();  
Options.insamplesize = 2;//Picture width is the original One-second, that is, the picture is the original One-fourth  
Bitmap Bitmap =bitmapfactory.decodestream ( Cr.openinputstream (URI), null, options); Preview.setimagebitmap (bitmap);

4, use the context of application to replace activity-related context-sensitive.
Try to avoid the context of the activity being used outside of its own scope, which can cause the activity to be released.
5), registration did not cancel the memory leak caused
such as: Broadcast
6), the object in the collection caused by the memory leaks we usually add references to objects to the collection, and when we don't need the object, we don't clean it out of the collection, so the collection gets bigger. If the set is static, the situation is even more serious.
7), handler should be declared a static object, and in its internal class to save a weak reference to the external classes. As follows:

Static class MyHandler extends Handler 
{
    weakreference<activity > mactivityreference;
    MyHandler (activity activity)
   { 
      mactivityreference= new weakreference<activity> (activity);
   }
   @Override public
   void Handlemessage (msg)
  {final activity activity
      = Mactivityreference.get ();
      if (activity!= null)
     {
         mimageview.setimagebitmap (mbitmap);
     }  
   }
}

2, the difference between ArrayList and LinkedList

    • ArrayList Initial size of 10, not enough size will call grow expansion: Length = length + (length >> 1)
    • Node First,last in LinkedList. Point to Tail

ArrayList and LinkedList have their own strengths and weaknesses in their performance, and they all have their respective applications, which can generally be described as follows:

1, for ArrayList and LinkedList, the overhead of adding an element at the end of the list is fixed. For ArrayList, the main thing is to add an item to the internal array, point to the added element, and occasionally cause the array to be reassigned, and for LinkedList, the overhead is uniform, assigning an internal entry object.
2, inserting or deleting an element in the middle of the ArrayList means that the remaining elements in the list are moved, while the overhead of inserting or deleting an element in the middle of the linkedlist is fixed.
3), LinkedList does not support efficient random element access.
4), ArrayList space waste is mainly reflected in the list at the end of a certain amount of space reserved, and linkedlist space expenditure is reflected in its every element needs to consume considerable space.
It can be said that when an operation is to add data after a column of data rather than in front or in the middle, and when you need to randomly access the elements, using ArrayList provides better performance; When you add or remove data in front of or in the middle of a column of data, and then access the elements in the order, You should use the LinkedList.
3, the difference between HashMap and Hashtable

1), inheritance is different.

public class Hashtable extends Dictionary implements map public class HashMap extends Abstractmap implements
map

The methods in

2) and Hashtable are synchronized, and the methods in HashMap are not synchronized by default. In the context of multithreading concurrency, you can directly use Hashtable, but to use hashmap words to increase their own synchronization processing. In
3), Hashtable, key and value do not allow null values.
in HashMap, NULL can be a key, with only one key, and a value of one or more keys can be null. When the Get () method returns a null value, you can either indicate that the key is not in HashMap or that the value of the key is null. Therefore, it is not possible in HashMap to determine whether a key exists in the HashMap by the Get () method, but rather by using the ContainsKey () method.
4), and two traversal methods are different on the internal implementation. The
Hashtable, hashmap all use iterator. And because of historical reasons, Hashtable also used the enumeration way.
5), the use of hash values is different, Hashtable directly use the object's hashcode. The hash value is recalculated by HashMap.
6.Hashtable and HashMap the initial size and capacity of the array of their two internal implementations. The default size of the hash array in Hashtable is 11, and the increase is old*2+1. The default size of the hash array in HashMap is 16, and must be an exponent of 2.
4, iterator, and enumeration different

1), function interface is different
Enumeration has only 2 function interfaces. With enumeration, we can only read the data in the collection, not modify the data. Iterator has only 3 function interfaces. Iterator can also delete data in addition to reading the collection's data.
2), iterator support fail-fast mechanism, and enumeration does not support. Enumeration is the interface that JDK 1.0 adds. The functions used to include vectors, Hashtable, and the like, which are all added to JDK 1.0, enumeration exist to provide them with traversal interfaces. The enumeration itself does not support synchronization, and when Vector, Hashtable implements enumeration, synchronization is added. The iterator is an interface that JDK 1.2 adds, and it also provides traversal interfaces for collections such as HashMap, ArrayList. Iterator is supported by the FAIL-FAST mechanism: When multiple threads operate on the contents of the same collection, Fail-fast events can occur.
The ail-fast mechanism is an error mechanism in the Java Collection (Collection). Fail-fast events can occur when multiple threads operate on the contents of the same collection. For example, when a thread a passes through a iterator to traverse a collection, if the content of the collection is changed by another thread, then thread a accesses the collection and throws a Concurrentmodificationexception exception to produce the Fail-fast event.

5, the attention point of the interface

1), the fields in the interface all default to the public static type.
2), the method in the interface all defaults to the public type.
3), the interface can declare the internal class, and the default is public static, because it is static, but the namespace belongs to the interface, code logic does not belong to the interface. So not illegal interface definition.
4), the interface itself can be declared public or default.
5), the abstract class inherits from an interface. If a method in the parent class (interface) is implemented in an abstract class, the subclass can be implemented without implementation, otherwise the subclass must implement it.
6. Final Method

There are two reasons to declare the method final. The first is that you already know that the functionality provided by this method already satisfies your requirements, does not need to be extended, and does not allow any class inherited from this class to overwrite this method, but inheritance can still inherit this method, which means it can be used directly. The second is to allow the compiler to convert all calls to this method into the inline invocation mechanism. It allows you to insert the method body directly into the call at the end of the call, rather than making routine method calls, such as saving breakpoints, pressing stacks, and so on, which may improve your program's efficiency. However, when your method subject is very large, or if you call this method in multiple places, your calling principal code expands rapidly and may affect efficiency, so be careful with final method definition.

Android Knowledge points

1. Handler mechanism

1), Handler effect on activity finish.
In the process of development encounter a thorny problem, Call the Activity.finish function acitivity does not perform the lifecycle of the Ondestory function, followed by half a day because there is a handler member, because it has a delay message is not processed, call activity.finish,activity will not immediately de Story, so remember to clean up the ativity in handle before the finish, so the activity will be smooth destory
2), Looper
Create the Looper () object and bind to the threadlocal variable by calling Looper.prepare ().
The Looper contains MessageQueue.
The constructor is as follows:

Private Looper ()
{
   mqueue = new MessageQueue ();
   Mrun = true;
   Mthread = Thread.CurrentThread ();
}

3), loop () function
1) Remove MessageQueue from Looper;
2) The loop extracts the message from the MessageQueue;
3 Remove target from message (handler object);
4) Call the Tartget dispatchmessage distribution message.
4), Handler object
Important member variables:

Final MessageQueue Mqueue;
Final Looper Mlooper;
Final Callback Mcallback;  For callback

The handler object sets MSG's target variable to itself when it sends a message. This allows the DispatchMessage () of the corresponding handler to be invoked when the Looper object loops out the MSG. The precedence of this function to distribute messages is as follows:
Message is created by calling obtain set callback.
Handler was introduced to callback at the time of creation.
Hand over to the handlemessage of the handler subclass (the usual practice).

2. Android Startup mode

Standard and Singletop mode.
These two are relatively simple. Create the activity into the current task stack, and if it is currently singleinstace, put it in the set task stack. If the activity is at the top of the stack, call onnewintent.

Singletask: In-stack reuse mode. Instead of looking for existence in the current task stack, the actual process is as follows:

1), find out whether the task stack required for the activity exists (controlled by taskaffinity or by default the package name).
2), in the task stack to find out whether the activity exists.
This is where the task stack is switched, which means that when the singtask type of activity that is opened does not belong to the current task stack, it switches to its task stack.
SingleInstance: Single instance mode.
Contains all the attributes of the Singletask, plus: The activity set to that pattern can only exist on a single task stack. When two singleinstace activity is set to the same task stack, there are two task stacks with the same name, each of which holds an activity of the same name.
Note: In any jump, first call the onpause of this activity, and then jump. If a jump activity does not create a new instance because of the startup method, the Onnewintent is called first, and then the normal lifecycle is invoked.
Such as
1:a→b,a:onpause;b:oncreate,onstart,onresume.
2: A (singletop) →a,a:onpause;a:onsaveinstancestate;a:onresume.

The above is I encountered and collected the various types of topics and corresponding solutions, the next period will continue to update, I hope you continue to pay attention.

I also wish you can find a job you like.

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.