1. What is activity?
one of the four components, general, a user interface corresponds to an activity
Setcontentview (),// the layout to display
, Activity is a subclass of the context and implements both Window.callback and Keyevent.callback, which can handle events that interact with the form user.
button.setonclicklinstener{
}
I develop commonly used listactivity, preferenceactivity, tabacitivty , etc...
if the interface has a common feature or function, it will define itself as a baseactivity.
Display and destruction of the progress dialog box Doubanservice
2. Please describe the activity life cycle.
The life cycle describes a method that a class executes from creation (new ) to death (garbage collection).
In this process, different methods will be called for different life stages.
Activity There are multiple states from creation to destruction, and the corresponding callback methods are fired from one state to another, including: OnCreate OnDestroy onstop onstart onresume onpause
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;
Popup of the dialog box, Activity.this
dialog boxes are part of the activity.
These 6 methods are relative, then only one Onrestart method is left, when is this method called?
The answer is: after the activity has been onstop, but not OnDestroy, when you start the activity again, call Onrestart (and no longer call OnCreate) method;
if It is OnDestroy, the OnCreate method is called.
Finally, the experience of the project, such as the watercress client every time you enter an interface to see the latest data, the refresh list of the operation is placed in the OnStart () method inside.
Filldata () This ensures that every time the user sees the data is up-to-date.
multimedia playback, play to the phone. OnStop () video, video sound set to 0, record the location of the video playback mediaplayer.pause ();
OnStart () Restore the site according to the saved state. Mediaplayer.start ();
While reading the document , it was also found that the activity had two methods for the life cycle of Onpostresume () and onpostcreate (), but it was not used during development.
3. 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 is not called
4. The life cycle of the activity when the screen is switched.
This life cycle is related to the configuration in the manifest file.
1 , when you do not set the activity 's android:configchanges, the screen will recall the various lifecycles
by default, the current activity is first destroyed and then reloaded
Onpause onstop ondestory oncreate onstart onresume
2 , when you set the activity 's android:configchanges= "Orientation|keyboardhidden", the screen does not recall the various lifecycles, Only the Onconfigurationchanged method is executed
in the game development, the screen faces are all written dead.
5. How to set an activity to the window's style.
You can customize the style of an activity, see details of the Mobile defender's program details
Android:theme= "@style/floatactivity"
E:\day9\mobilesafe\res\values\style
6. What happens to your back-office activity being collected by the system? How can I save the current state before being reclaimed by the system if the activity in the background is reclaimed by the system for some reason?
In addition to the activity at the top of the stack, other activity is likely to be reclaimed by the system when the memory is low, the more the activity is at the bottom of the stack, the greater the likelihood of being recycled.
protected void onsaveinstancestate (Bundle outstate) {
Super. Onsaveinstancestate (outstate);
Outstate.putlong ("id", 1234567890);
}
Public void onCreate (Bundle savedinstancestate) {
// determine if Savedinstancestate is empty.
// if it's not empty, take it out.
Super. OnCreate (savedinstancestate);
}
7. How do I exit activity? How do I safely exit a application that has called multiple activity?
exit activity calls the finish () method directly. The user clicks the Back button to exit an activity
exiting activity executes the OnDestroy () method.
1 , throw exception forced exit:
This method makes the program force Close by throwing an exception.
validation is possible, however, the problem that needs to be addressed is how to make the program end without ejecting the Force Close window.
// Secure End Process Android.os.Process.killProcess (Android.os.Process.myPid ());
2 , log the activity that is open:
each activity is openedand recorded. When you need to exit, close each activity.
list<activity>lists; in the context of the complete application
Lists =new arraylist<activity> ();
Each activity is executed at the time of the OnCreate () method Lists.add (this);
Ondestory () Lists.remove (this);
Lists.add (activity);
for (Activity activity:lists)
{
Activity.finish ();
}
3 , 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.
// to register an activity with the intention of receiving a broadcast
Registerreceiver (Receiver,filter)
// If you have received a broadcast that closes activity, call the finish () method to drop the current activity finish ()
4 , recursive exit
Use Startactivityforresult when opening new activity , then flag it yourself, process it in Onactivityresult, and turn it off recursively.
404 Exit Program
Onactivityresult ();
Tell me about your understanding of activity.
Write the points above with your own experience.
8. Does the service execute in the main thread, and does the service have time-consuming operations?
by default, if there are no processes running for the specified service, the service and activity are running in the main thread (UI main thread) of the process where the current app is located
Service It is not possible to perform time-consuming operations ( network requests, copy databases, large files)
Executes the new thread () {}.start () in the child thread;
Special case, you can configure the process where the service executes in the manifest file,
Let the service execute in a different process
<service android:process="cn.itcast.xxx"></service>
9. How do I pass data between two activity?
The base data type can be passed. Intent Passing Data
Extras.putdouble (key, value)
Intent.putextra (Name,value)
// through Intentputextra Method Basic data Types are passed
Bundlebundle = new Bundle ();
Bundle.putshort (Key,value);
Intent.putextras (bundle);
Intent.putextras (bundle)
get to activate his getintent ();
Intent Intent = Getintent ();
Bundle Bundle= Intent.getextras ();
Bundle similar Map the collection
Intent.getstringextra ("Key", "value");
Intent.getbooleanextra ("Key", "value")
Application In the Global store objects, to implement their own application of this class, the basic system of application, each activity can be taken to
let the object implement implements Serializable The interface stores the object on the file.
let the class implement Serializable Interface , you can then pass ObjectOutputStream// Object Output Stream
File File = newfile ("C:\\1.obj");
FileOutputStream fos = new fileoutputstream (file);
ObjectOutputStream Oos = newobjectoutputstream (FOS);
Student stu = newStudent ();
Stu.setid ("10001");
Stu.setname ("Zs");
Oos.writeobject (Stu);
FileInputStream FIS = newfileinputstream (file);
ObjectInputStream ois = newobjectinputstream (FIS);
Student stu1 = (Student) ois.readobject ();
System. out. println (Stu1.getname ());
parcelable and the Serializable
parcelable serializes the object to Android Operating System a common piece of memory space
File/ Network
Passing a reference to the data
Intent.setdata (Uri)
Uri.fromfile (); transfer of large pictures
Contentresolver.getinputstream (URI);
10. How do I start a service when an activity is started?
Inside the OnCreate () method of the activity startservice ();
android--Surface Examination Questions finishing (I.)