Common practice skills in activities

Source: Internet
Author: User

1 //know which activity is currently in2 /*Create a new Baseactivity class (Java Class) that inherits from Appcompatactivity3 * Override the OnCreate () method, the existing activity does not have to inherit from the Appcompatactivity, but inherits from the baseactivity, when jumps to an activity interface, the Toast message box displays the activity name4  */5  Packagecom.example.activitytest;6 7 Importandroid.app.Activity;8 ImportAndroid.os.Bundle;9 Importandroid.support.annotation.Nullable;Ten Importandroid.support.v7.app.AppCompatActivity; One ImportAndroid.util.Log; A ImportAndroid.widget.Toast; -  - /** the * Created by admin on 2017/1/22. -  */ -  -  Public classBaseactivityextendsappcompatactivity { + @Override A     protected voidonCreate (@Nullable Bundle savedinstancestate) { at         Super. OnCreate (savedinstancestate); -  -         //displays the name of the current activity with the toast message prompt -Toast.maketext (baseactivity. This, GetClass (). Getsimplename (), Toast.length_short). Show (); -     }
1 /*quit programs anytime, anywhere2 * Implement the Activitycollector class as an activity manager3  */4  Packagecom.example.activitytest;5 6 Importandroid.app.Activity;7 8 Importjava.util.ArrayList;9 Importjava.util.List;Ten  One /** A * Created by admin on 2017/1/22. -  */ -  the  Public classActivitycollector { -     /* - staging an activity with a list -      */ +      Public Staticlist<activity> activities =NewArraylist<>(); -     //provides a addactivity () method to remove an activity from the list. +      Public Static voidaddactivity (activity activity) { A Activities.add (activity); at     } -      Public Static voidremoveactivity (activity activity) { - activities.remove (activity); -     } -  -     //Finally, a Finishall () method is provided to destroy all the activities stored in the list in     //no matter where you want to quit the program, just call Activitycollector.finishall (). -      Public Static voidFinishall () { to          for(Activity activity:activities) { +             if(!activity.isfinishing ()) { - activity.finish (); the             } *         } $     } - } the  + //Modify the code in the Baseactivity class A  Packagecom.example.activitytest; the  + Importandroid.app.Activity; - ImportAndroid.os.Bundle; $ Importandroid.support.annotation.Nullable; $ Importandroid.support.v7.app.AppCompatActivity; - ImportAndroid.util.Log; - ImportAndroid.widget.Toast; the  - /**Wuyi * Created by admin on 2017/1/22. the  */ -  Wu  Public classBaseactivityextendsappcompatactivity { -  About @Override $     protected voidonCreate (@Nullable Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); -         //indicates that the currently created activity is added to the activity manager. -Activitycollector.addactivity ( This); AToast.maketext (baseactivity. This, GetClass (). Getsimplename (), Toast.length_short). Show (); +     } the @Override -     protected voidOnDestroy () { $         Super. OnDestroy (); theActivitycollector.removeactivity ( This); the     } the}
1 /* Best practices for initiating activities 2 * You will encounter a problem: in your own responsible section you need to start the function of secondactivity this activity,3 * But it's not clear that this activity needs to pass that data (the code that takes someone else in the project)4 * Modify the code in the Secondactivity5  */6  Public Static voidACTIONStart (context context, string data1, String data2) {7Intent Intent =NewIntent (context, secondactivity.class);8Intent.putextra ("param1", data1);9Intent.putextra ("param2", data2);Ten context.startactivity (intent); One     } A  - /*this way, even if you do not have to read the secondactivity code, do not ask the responsible secondactivity - * Written by colleagues, can also be very clear direct to start secondactivity need to pass what data. the * Starting secondactivity in active firstactivity requires only a small line of code -  */ -Secondactivity.actionstart (firstactivity. This, "Data1", "data2");

Learning Books: The first line of code

Common practice skills in activities

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.