Inherit the application management life cycle

Source: Internet
Author: User

Inherit application for Android data sharing

Http://www.jianshu.com/p/75a5c24174b2

Jessyan proposes a way to manage the extended lifecycle of each activity with the application + interface

What's the use of this interface?

Application provides a method that has a registeractivitylifecyclecallbacks () that needs to pass in the parameter is this Activitylifecyclecallbacks interface, function and you guessed right, is that after you call this method to pass through the interface implementation class, the system will be in each Activity After executing the corresponding life cycle, call the corresponding method in this implementation class, and remember that it is each!

This time we will think of a need to achieve, close all Activity ! You're still inheriting baseactivity in baseactivity 's onCreate . Add this Activity to the collection???

I now tell you such a disadvantage, if you open in the App has other three-party library Activity , this tripartite library certainly cannot inherit your baseactivity , then what do you do?

This is where activitylifecyclecallbacks comes in handy, and all Activity in the App will call the corresponding method of the interface implementation class as long as the life cycle is completed. Then you can add all the activity to the collection in onactivitycreated , so that whether you're a third-party activity or not, I can traverse the collection. All the Activity


Now in accordance with his ideas to achieve toolbar First definition of toolbar XML, I used the V7 toolbar
<?xml version= "1.0" encoding= "Utf-8"?><android.support.v7.widget.Toolbar    android:id= "@+id/ Toolbar "    xmlns:android=" http://schemas.android.com/apk/res/android "    xmlns:app=" http ://schemas.android.com/apk/res-auto "    android:layout_width=" Match_parent "    android:layout_ Height= "? attr/actionbarsize"    android:layout_gravity= "Top"    Android:background = "? Attr/colorprimary"/>

Using toolbar

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" Match_parent "    android:layout_height = "Match_parent" >    <include layout = "@layout/app_toolbar"/></linearlayout>

Inheriting application, overriding interfaces

 Public classLapplicationextendsApplication {@Override Public voidonCreate () {Super. OnCreate (); Registeractivitylifecyclecallbacks (Newactivitylifecyclecallbacks () {@Override Public voidonactivitycreated (activity activity, Bundle savedinstancestate) {if(Activity.findviewbyid (r.id.toolbar)! =NULL) {                    if(Activityinstanceofappcompatactivity) {Toolbar Toolbar=(Toolbar) Activity.findviewbyid (R.id.toolbar);                        ((appcompatactivity) activity). Setsupportactionbar (toolbar); ((appcompatactivity) activity). Getsupportactionbar (). setdisplayshowtitleenabled (false);                    Toolbar.settitle (Activity.gettitle (). toString ()); }                }            }    }}

This will call the application code when the activity is super.oncreate.

 Public class extends appcompatactivity {    @Override    protectedvoid  onCreate (Bundle Savedinstancestate) {        setcontentview (r.layout.activity_main);         Super . OnCreate (savedinstancestate);    }}

So pay attention to Setcontentview before calling Super.oncreate.

Last Set Application

<application android:name= ". Lapplication "

Finish the effect:

Inherit the application management life cycle

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.