10 days to learn Android-seventh day

Source: Internet
Author: User

We learned the last time Baidu positioning and the use of sharedpreferences, do not know if you have noticed that we added a new method:

    protected void OnStop ()    {        super.onstop ();        Mlocationclient.stop ();    }

The function of this method is to stop the interface at the same time and stop the Baidu positioning function.

Think of us and oncreate, so what are these two methods for? What is the principle?

This requires us to look at the life cycle of the activity.

Activity life cycle

The whole life cycle of activity has seven important methods, namely OnCreate, OnStart, Onresume, OnPause, OnStop, Onrestart, OnDestroy, and these seven methods will have different triggering time in different states. ,

1. When activity starts, OnCreate, OnStart, and Onresume are executed in turn

2. When the activity loses focus, OnPause and OnStop are executed in turn

3. When the activity is re-focused, Onrestart, OnStart, and Onresume are executed in turn

4. When activity is closed, OnPause, OnStop, and OnDestroy are executed in turn

As an example:

When the app has been started, if you press the back key, then the 4th type-off, will be executed OnPause, OnStop and OnDestroy,

Press the Home button, then is the 2nd-lost focus, will be executed OnPause and onstop, this time if you start the app again, will be in accordance with the 3rd-regain focus, will be executed in turn Onrestart, OnStart and Onresume.

To verify this, we modify the Mainactivity.java and add the following methods:

    @Override    protected void OnPause ()    {        log.v ("Weatherapp", "OnPause");        Super.onpause ();    }    @Override    protected void Onrestart ()    {        log.v ("Weatherapp", "Onrestart");        Super.onrestart ();    }    @Override    protected void Onresume ()    {        log.v ("Weatherapp", "Onresume");        Super.onresume ();    }    @Override    protected void OnStart ()    {        log.v ("Weatherapp", "OnStart");        Super.onstart ();    }    @Override    protected void OnDestroy ()    {        log.v ("Weatherapp", "OnDestroy");        Super.ondestroy ();    }

and add separately in the OnCreate and OnStop methods:

        LOG.V ("Weatherapp", "onCreate"); LOG.V ("Weatherapp", "onStop");

Then run the program, try repeating the above, and view the corresponding log in Logcat view.

Here's a little tip, click the green plus sign in the upper left corner of the Logcat view to add a custom log filter, and I'm using this feature here.

After understanding the life cycle of activity, how do they actually apply in the actual app?

So, give some examples.

1. If our weather app needs to get the latest weather every time the user is re-visible , the call to get weather-related code needs to be placed in the Onresume method

2. Another situation, our app is already visible in the foreground, when the phone came, it became invisible, then hung up and re-visible, this time usually do not need to retrieve the data, then call to get weather-related code needs to be placed in the OnStart method

3. In the simplest case, we only need to get the weather at boot time, then we just need to execute it in the OnCreate method

So, technology is not a problem, everything needs to be based on the actual needs to determine how our program should be implemented.

The above is a brief introduction of the activity of the life cycle, we can slowly grope in practice, and the above is only the basic content, the higher level of the content we will be in the later study slowly contact.

After understanding the life cycle, do you want to make some changes to our app, well, just follow the 2nd situation to modify it.

All you have to do is move the following code from the OnCreate method to the OnStart.

        String city = readcity ();        if (city = null && city.length () > 0)        {            getWeather (city);        }

See if the result is what you want.

Presumably practiced here, everyone is tired, theoretical knowledge is really annoying, but it is really what we must, only after the theory of armed knowledge can play the greatest role.

Come here today, you guys are working hard.

Tomorrow is the weekend, there will be a lot of content, please look forward to!

This series of articles is my original, if need to reprint, please specify the source www.liuzhibang.cn

10 days to learn Android-seventh day

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.