Android Learning notes? From scratch? The third day? Activity

Source: Internet
Author: User

Android Learning notes? From scratch? The third day? Activity

Reprint Please specify source : Cleopard Http://blog.csdn.net/cleopard

Today is mainly the study of activity.
–> Click to enter the Android learning Note Map http://blog.csdn.net/cleopard/article/details/44037813
–>19 Android Classic Tutorial + 104 hot item on github with open stream code http://download.csdn.net/album/detail/1511

Life cycle of activity

There are seven callback methods defined in the activity class, covering every aspect of the active life cycle, and let me introduce each of the seven methods below.

1. OnCreate ()

You have seen this method many times, and we have rewritten this method in each activity, and it will be called when the activity is first created. You should perform initialization of activities in this method, such as loading layouts, binding events, and so on.

2. OnStart ()

This method is called when the activity becomes visible from invisible.

3. Onresume ()

This method is called when the activity is ready to interact with the user. The activity at this point must be at the top of the stack that is returned and is in a running state.

4. OnPause ()

This method is called when the system is ready to start or resume another activity. We usually release some CPU-consuming resources in this method and save some key data, but this method must be executed quickly, otherwise it will affect the use of the new stack top activity.

5. OnStop ()

This method is called when the activity is completely invisible. The main difference between it and the OnPause () method is that if the new activity started is a dialog-box activity, then the OnPause () method will be executed, and the OnStop () method will not execute.

6. OnDestroy ()

This method is called before the activity is destroyed, and then the state of the activity becomes the destroy state.

7. Onrestart ()

This method is called before the activity is changed from the stopped state to the running state, i.e. the activity is restarted.
In addition to the Onrestart () method in the above seven methods, the others are 22 relative, and the activity can be divided into three lifetimes.

1. Full lifetime

The activity between the OnCreate () method and the OnDestroy () method is the complete survival period. In general, an activity completes various initialization operations in the OnCreate () method, while freeing the memory in the OnDestroy () method.

2. Visible lifetime

The activity that is experienced between the OnStart () method and the OnStop () method is the visible lifetime. During the visible lifetime, activities are always visible to the user, even though they may not be able to interact with the user. Through these two methods, we can reasonably manage the resources that are visible to the user. For example, the resource is loaded in the OnStart () method, and the resource is freed in the OnStop () method to ensure that the inactive activity does not consume too much memory.

3. Foreground lifetime

The activity between the Onresume () method and the OnPause () method is the foreground lifetime. In the foreground life period, the activity is always in the running state, at this time the activity is can and the user to each other, we usually see and contact the most also this state of activity.
The following process is a visual representation:

If you have any questions, you can refer to this blog post:
http://blog.csdn.net/Android_Tutor/article/details/5772285

Insufficient memory to save data before recycling activity

The Onsaveinstancestate () method can be called to prevent the system from running out of memory and reclaim the activity that holds the temporary data.

    @Override    protectedvoidonSaveInstanceState(Bundle outState){        super.onSaveInstanceState(outState);        String tempData=”tempData”;        outstate.putString(“data_key”,temData);    }

Get the stored data from the activity's OnCreate () method:

    if(savedInstanceState!=null){        String tempData=savedInstanceState.getString(“data_key”);    }

*intent can be used in conjunction with bundles to pass the data, first you can save the data to be passed in the bundle object, and then store the bundle object in the intent;

Activation mode of activity

There are four types of start-up modes: Standard, Singletop, Singletask, SingleInstance.
You can assign a property to a tag in Androidmanifest.xml <activity> android:launchMode .
Standard: Normal mode, if the creation of activity will be created continuously, such as in firstactivity there is a button to open firstactivity, a continuous click 3 times the button, you need to press the return key 3 times to exit.
singletop: Activity at the top is not created repeatedly, but not at the top is recreated.
singletask: As long as the existing activity is not recreated.
singleinstance: Restart the stack.

Small tricks that can play an extension. Activity Tips 1-Know what's currently active

New base class Baseactivity inherits activity, overrides Baseactivity onCreate() method, adds Log.d(“BaseActivity”,getClass().getSimpleName());
Other activity inherits this class.

Activity Tips 2-quit the program at any time

If you open multiple pages, press the back key multiple times to exit, press the home key to just hang the program.
Create a new ActivityCollector class as the activity Manager:

 Public classactivitycollector{ Public Staticlist<activity> activities =NewArraylist<activity> (); Public Static void addactivity(Activity activity)    {Activities.add (activity); } Public Static void removeactivity(Activity activity)    {Activities.remove (activity); } Public Static void Finishall(){ for(Activity activity:activities) {if(!activity.isfinishing ())            {activity.finish (); }        }    }}

In the method of the previous technique, BaseActivity中onCreate() add:

ActivityCollector.addActivity(this);

Re-rewrite the baseactivity onDestroy() :

@OverrideprotectedvoidonDestroy(){    super.onDestroy();    ActivityCollector.removeActivity(this);}

Call the method regardless of where you want to exit ActivityCollector.finishAll() .

Of course, after destroying all active code, add the code that kills the current process to ensure that the program exits completely.

Activity Tips 3-Starting the best method

Assume that the Secondactivity boot requires two string arguments.
To modify the code in Secondactivity:

publicclass SecondActivity extends BaseActivity{    publicstaticvoidactionStart(Context context, String sata1, String data2){        new Intent (context, SecondActivity.class);        intent.putExtra(“param1”,data1);        intent.putExtra(“param2”,data2);        context.startActivity(intent);    }}

This at a glance, reflects all the data secondactivity need, not how to read the Secondactivity code to know. and simplify the code. Improve team development efficiency. All you need is one sentence:

SecondActivity.actionStart(FirstActivity.this,”Hello”,”CLeopard”);
This is the end of today 2015/03/13 13:15 Cleopard
Reprint Please specify source : Cleopard Http://blog.csdn.net/cleopard

Android Learning notes? From scratch? The third day? 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.