Android Four components------Activity

Source: Internet
Author: User
Tags throw exception

I. Defining the main activity that the software opens

The OnCreate () method in an activity declared as "launch" (or "main") in the app is called by default when the user opens the software. We typically define the main activity in Androidmanifest.xml.

<activity android:name= ". Mainactivity "android:label=" @string/app_name ">    <intent-filter>        <action android:name=" Android.intent.action.MAIN "/>        <category android:name=" Android.intent.category.LAUNCHER "/>    < /intent-filter></activity>

Interview questions related to activity:

1. If the new activity is a transparent theme, will the old activity go onstop  

  No! The old activity is in the paused state.

2.what is taskaffinity?

Each activity has a taskaffinity attribute, which indicates the task it wants to enter. Attribute that identifies the activity task stack name: taskaffinity, which defaults to the app package name.

3.What is the mode of communication between Android service and activity?

    • Binder
    • Broadcasting
    • Eventbus

4.Activity Start Mode

    • Intent
    • ADB Shell AM Instruction

    AM initiates an activity:

ADB shell am start com.example.fuchenxuan/. Mainactivity

AM sends a broadcast using the action:

ADB shell am broadcast-a Magcomm.action.TOUCH_LETTER

5. How do I get the current screen activity?

  Use Activitylifecyclecallbacks, detailed.

Four startup modes in 6.Activity?

  The starting mode has 4 kinds, namely standard, Singletop, Singletask, singleinstance;

    • stand ARD: Each time the activity is activated (startactivity), the activity instance is created and put into the task stack;
    • singletop: If an activity activates itself, that is, the activity at the top of the task stack, you do not need to create it, and the rest creates an activity instance;
    • singletask: If the activity that you want to activate is present in the task stack, you do not need to create it, just put this activity on top of the stack and pop the activity instances above the activity.
    • singleinstance: If the mainactivity instance is created in the task stack of application 1, if Application 2 also activates mainactivity, there is no need to create and two apps to share the activity instance;

      

7. How do I exit the activity? How do I safely quit a application that has called multiple activity?

To exit a single activity:
    • Normally the user exits an activity simply by pressing the return key, and we write the code to exit the activity directly by calling the finish () method on the line.
    • Kill the Process: KillProcess (Process.mid)
    • To terminate a running virtual machine: System.exit ()
Exit the entire application:

    • Manufacturing throw exception causes the entire program to exit
    • To 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)

    //如果过接受到的是 关闭 activity 的广播 activity finish()掉

    • Recursive exit

Call the Finish () method to use the current Startactivityforresult when opening a new Activity, then flag it yourself, process it in Onactivityresult, and turn it off recursively.

    • Intent.setflags (intent.flag_activity_clear_top) activates a new ACTIVITY through the intent flag. At this point, if the activity is already in the task stack, the system will kill all the activity on the activity. In fact, the equivalent to the Activity configuration of the startup mode is Singletop.
    • Log the activity that is open:

Each activity is opened and recorded. When you need to exit, close each Activity

  

Appendix: Activity Life Cycle:

Android Four components------Activity

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.