1, access to the private member variable or method, you need to execute the setaccessible () method, and set the entry parameter to true; Otherwise, access is not allowed.
2, in order to ensure thread security, you can use the synchronization block synchronized keyword. There is also a way to define synchronization, which is preceded by a synchronized.
1
Synchronized(Object) { ... // }//2publicsynchronizedvoid doit () { // Place the shared resource operation here }
3, about the try Catch finaly
First look at the following program:
protected boolean Fina11 () { try {Toast . Maketext ( this , "You clicked Add" , Toast.length_short). Show (); return true ; catch (Exception e) {} finally this ," finally " return false ; }
The result of the run after execution is "you clicked Add", "finally", the return value is true; not false; If the return true is commented out, then the return is false;
4, can send intent to the service side in the Widegt, the service side registration accepts can, but sends the activity the word to be somewhat difficult.
5, under normal circumstances requestwindowfeature (Window.feature_no_title) can be effective, but when the activity inherits the Appcompatactivity, this is invalid.
The first solution, add the Red code, note to put in the last:
protected void onCreate (Bundle savedinstancestate) { super. OnCreate (savedinstancestate); Requestwindowfeature (window.feature_no_title); Setcontentview (R.layout.activity_main); if (getsupportactionbar () = null) { getsupportactionbar (). Hide (); } }
The second solution, add red code, note the code order, put in the middle, other locations can not:
protected void onCreate (Bundle savedinstancestate) { super. OnCreate (savedinstancestate); supportrequestwindowfeature (window.feature_no_title); Setcontentview (R.layout.activity_main); }
6, about the implementation of adding a click event to Recycleview:
First we implemented the implements View.onclicklistener for it, and then overridden the OnClick event. In the onclick we make a call to the method Onitemclick in the interface Onitemclicklistener. This enables the Click Item event. See Https://github.com/huanshen/Learn-Android/tree/master/recycleTest for specific code. Download the compile and run.
In fact, as long as the implementation of implements View.onclicklistener, click on the OnClick method will be called, but because it is the global corresponding, not an item, so we re-write an interface, and defined a onitemclic K method.
7. String.valueof and. ToString methods
First we look at the source of string.valueof:
Public Static String valueOf (Object obj) { returnnull)? "Null" : Obj.tostring ();}
It can be found that the implementation of the string.valueof is actually called the. ToString method, except that it also handles the case where the data is empty. Returns "NULL".
8, in the actual application scenario, suppose a activity is located at the top of the stack, at this time the user operation, from a activity jump to B activity. So what are some of the life cycle methods that will be recalled for AB? What is the specific callback order of the callback method?
At the beginning, A is instantiated and the callback executed is A:oncreate, A:onstart, A:onresume.
When the user clicks a in the button to B, assuming that B is all blocked a, will be executed A:onpause, b:oncreate, B:onstart, B:onresume, A:onstop.
At this point, if you click the back key, you will perform B:onstop, B:ondestroy, A:onresume, A:onstart, B:onpause, A:onrestart, and so on.
At this point, only a is in the activity stack. In Android, there are two keys that affect the activity life cycle in particular, the back key and the home key. Let's look directly at the results of the experiment:
If you press the back key at this point, the system returns to the desktop, followed by A:onpause, A:onstop, A:ondestroy.
If you press the home key (not the long Press), the system returns to the desktop and executes A:onpause-a:onstop in turn. Thus, the main difference between the back key and the home key is whether the OnDestroy will be executed.
At this point, if you press and hold the home button, different phones may pop up different content, the activity life cycle has not changed (measured by millet 2s, do not know whether the other phone will affect the activity life cycle).
9. The advantages and disadvantages of IPC communication methods and the applicable scenarios
10. Multi-process mode in Android
(1) You can open the multi-process mode by assigning properties to the four components, the process name of the android:process default process is the package name PackageName, the process name begins with: The process is the private process of the current application, the other application's components cannot run in the same process as it, and the process name does not : The beginning of the process belongs to the global process, and other applications ShareUID can run in the same process with it through the method.
// the process name is PACKAGENAME:XYZ // the process name is AAA.BBB.CCC
(2) The Android system assigns a unique UID to each app, and the app with the same UID can share the data. Two applications running through Shareuid are required in the same process, requiring both applications to have the same shareuid and signatures to be the same. In this case, they can access each other's private data, such as the data directory, component information, and so on, regardless of whether they run in the same process. If they run in the same process, they can also share memory data, which looks like two parts of an app.
(3) The Android system assigns a separate virtual machine to each process, and different virtual machines have different address spaces on the memory allocation, so objects in different virtual machines that access the same class produce multiple replicas.
(4) The use of multithreading is easy to cause the following problems:
1. Static members and Singleton mode are completely invalid;
2. The thread synchronization mechanism fails completely: the thread synchronization cannot be guaranteed regardless of the lock object or the lock global object;
3. SharedPreferences decreased reliability: Sharedpreferences does not support concurrent read and write;
4. Application will be created multiple times: When a component runs in a new process, the system allocates a separate virtual machine while the new process is created, and the application restarts once, creating a new application. Components running in the same process belong to the same virtual machine and the same application.
Different components of the same application, if they are running in different processes, are not fundamentally different from the two applications they belong to.
Android and Java Learning notes