Android interview questions (2)

Source: Internet
Author: User

2. Change Activity to a window: Activity attribute setting

It's easy to talk about it. Some people may want to make the app something floating on the main interface of the mobile phone.
You only need to set the Activity topic to define the Activity in AndroidManifest. xml.
Local sentence:

Xml Code

Android: theme = "@ android: style/Theme. Dialog"

Android: theme = "@ android: style/Theme. Dialog"

This makes your application pop up in the form of a dialog box, or

Xml Code

Android: theme = "@ android: style/Theme. Translucent"

Android: theme = "@ android: style/Theme. Translucent"

It becomes translucent. [friendly reminder -. -] attributes of similar activities can be found in android. r. the AndroidManifestActivity method of the styleable class shows AndroidManifest. for more information about the attributes of all elements in xml, see android. r. styleable

The above is the property name. The specific value is in android. r. style, such as @ android: style/Theme. dialog "corresponds to android. r. style. theme_Dialog, (replace '_' '. '<-- Note: The content of this article is not a smiley face. You can use it in the description file to find the ing between the class definition and the description file.

3. What should I do if your background Activity is recycled by the system: onSaveInstanceState?

When one Activity A in your program is running, it actively or passively runs another Activity B
At this time, A will execute

Java code

Public
Void onSaveInstanceState (Bundle outState ){
Super. onSaveInstanceState (outState );
OutState. putLong ("id", 1234567890 );
}

Public void onSaveInstanceState (Bundle outState) {super. onSaveInstanceState (outState); outState. putLong ("id", 1234567890 );}

B will come to A again after completion. There are two situations at this time: A is recycled, and A is not recycled.
The onCreate () method must be re-called for the received A. It is different from directly starting the onCreate () method with parameters.
SavedInstanceState. If it is not recovered, it will be onResume.

SavedInstanceState is a Bundle object. You can basically regard it as a Map object maintained by the system. You may use it in onCreate (). If onCreate () is started normally, it will not be available. Therefore, you need to determine whether it is empty when using it.

Java code

If (savedInstanceState! = Null ){
Long id = savedInstanceState. getLong ("id ");
}

If (savedInstanceState! = Null) {long id = savedInstanceState. getLong ("id ");}

Just like in the official Notepad tutorial, if you are editing a note that is suddenly interrupted, remember the note id, then, you can extract the note according to the id, and the program will be complete. This also shows that your application does not need to be saved. For example, if your interface is to read a list, you do not need to remember anything special. Oh, maybe you need to remember the position of the scroll bar...

4. call and call: our communication messenger Intent

Intent is the intention. Intent communicates with each other, makes a call, and makes a call.
Intent will be sent over the phone. This is the essence of loose coupling of the Android architecture, which greatly improves the reusability of components. For example, you need to click a button in your application, it's easy to call someone. Let's look at the code first:

Java code

Intent intent = new Intent ();
Intent. setAction (Intent. ACTION_CALL );
Intent. setData (Uri. parse ("tel:" + number ));
StartActivity (intent );

Intent intent = new Intent (); intent. setAction (Intent. ACTION_CALL); intent. setData (Uri. parse ("tel:" + number); startActivity (intent );

Throwing out such an intention, the system will wake up the dialing program and make a call when it sees your intention. What reading contacts, text messages, emails, all just need to be thrown out of intent. This part is really well designed.

So what does Intent tell the system who needs to accept it?
There are two methods to use Intent. The first method is to directly describe which class is required to receive the Code as follows:

Java code

Intent intent = new Intent (this, MyActivity. class );
Intent. getExtras (). putString ("id", "1 ");
StartActivity (intent );

Intent intent = new Intent (this, MyActivity. class); intent. getExtras (). putString ("id", "1"); tartActivity (intent );

The first method is obvious. You can directly specify MyActivity as the receiver and pass some data to MyActivity. In MyActivity, you can use getIntent () to get the intent and data.

In the second method, you need to first check the intentfilter configuration in AndroidMenifest.

Xml Code

<Intent-filter>
<Action
Android: name = "android. intent. action. VIEW"
/>
<Action
Android: value = "android. intent. action. EDIT"
/>
<Action
Android: value = "android. intent. action. PICK"
/>
<Category
Android: name = "android. intent. category. DEFAULT"
/>
<Data
Android: mimeType = "vnd. android. cursor. dir/vnd. google. note"
/>
</Intent-filter>

<Intent-filter> <action android: name = "android. intent. action. VIEW "/> <action android: value =" android. intent. action. EDIT "/> <action android: value =" android. intent. action. PICK "/> <category android: name =" android. intent. category. DEFAULT "/> <data android: mimeType =" vnd. android. cursor. dir/vnd. google. note "/> </intent-filter> actions, data, and category are used in the configuration. Therefore, you must be smart enough to think that intent also has these things, after a match, won't I find the receiver?

Action is actually a string name of intent.
The configuration file of intent-filter in the above section shows that this Activity can accept different actions. Of course, the corresponding program logic is different. Let's mention the mimeType, which is defined in ContentProvider, if you implement a ContentProvider by yourself, you must specify mimeType to enable data to be used by others.

I don't know the principle. I don't want to explain it. In summary, you call another interface instead of the new interface. Instead, you can throw an intent to let the system call that interface for you, in this way, it is loose and compliant with the principle that the lifecycle is managed by the system.

If you want to know what category has and what actions Android has customized for you, visit the official link Intent.

Ps: If you want to know how to call system applications, you can take a closer look at your logcat. Do you have some information when running a program? For example:
Starting activity: Intent {action = android. intent. action. MAINcategories = {android. intent. category. LAUNCHER} flags = 0x10200000comp = {com. android. camera/com. android. camera. galleryPicker }}
Compare some set methods of Intent to know how to call them. Hope you like it :)

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.