A messy summary of Android activity

Source: Internet
Author: User

Activity in Android can be said to be the broadest and most basic component of the four components. Almost all interactions with the user need to be done through activity.

Recently in the learning process, there are some content about activity, let me confused. So summarize this in order to make my knowledge more solid.

1.Activity Life cycle:

The usual life cycle a look at the picture, all understand, but today encountered a problem, let me very is no solution, checked the long time, only to find the reason.

At first I understand that when a acitivity force is set to horizontal screen, and then press the power button to lock screen, will not go back to OnCreate, and later found that the wrong, because the phone is a vertical screen, when you lock screen, in fact, the equivalent of switching to vertical screen, So we're going to call OnCreate. And then there's the question of how to block the problem. In the Mainfest file, configure android:configchanges= "Orientation|keyboardhidden" for the activity. It should have been said, but tried for a half day or not. So search a lot, only found in API 13, need to add screensize, so the correct should be android:configchanges= "Orientation|keyboardhidden|screensize"

2.Activity Best Practices

Recently bought this Guo Lin "first line code" although the content is relatively basic, but still have a lot of things to learn. For example, he mentions the best practices for activity initiation.

Problem Description:

When two people or a team to complete a project, often each person is responsible for a plate, or function module, if you want to call someone else to write activity, and I do not know what parameters should be passed to it, so that every time to ask the person responsible for the code, or directly see how he wrote. This kind of collaboration is a bit less efficient. So, the author of this book provides a way. As below, you can understand it at a glance

Public class example extends activity{public   STAITC void ACTIONStart (context context, String p1,string p2)   {
   
    intent Intent = new Intent (content,this.class);          Intent.putstring ("Parm1", p1);          Intent.putstring ("Parm2", p2);          Context.startactivity (intent);    }      }
   

  

3. Start activity in Broadcastreciever

When we receive the message in the broadcast receiver, we may need to start the app activity according to the situation, but when I take it for granted in the normal way, I report a run-time error, presumably meaning that if you start activity on the radio, The intent flag must be specified as Flag_activity_new_task

Must be specified as NewTask in flag.

The relationship between Launchmode in 4.Activity and flag in intent

After I check the information and look at some of the Netizen's things, the preliminary draw such a conclusion.

The Launchmode is primarily for behavior in the same task.

The flag indicates whether Activiy needs to create a new task.

Whether or not to create a new depends on whether Acitivy's taskaffinity is the same as it already exists. (By default, all Activiy taskaffinity in a program are the same)

A messy summary of Android activity

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.