My Android Step-by-step tour------>android A simple package for activity management

Source: Internet
Author: User

How to manage the current running activity stack, how to completely exit the program, this article encapsulates an activity management class, you can easily quit the program at any time.

Import Java.util.stack;import Android.app.activity;import Android.content.context;public class ActivityManager { private static stack<activity> activitystack;private static Activitymanager instance;static {instance = new Activitymanager ();} public static Activitymanager getinstance () {return instance;} /** * Add specified activity to stack */public void addactivity (activity activity) {if (activitystack==null) {activitystack=new stack <Activity> ();} Activitystack.add (activity);} /** * Get Current Activity */public Activity currentactivity () {activity activity=activitystack.lastelement (); return activity;} /** * End Current Activity */public void finishactivity () {activity activity=activitystack.lastelement (); Finishactivity ( activity);} /** * Ends the specified activity */public void finishactivity (activity activity) {if (activity!=null) {Activitystack.remove (activity ); Activity.finish (); activity=null;}} /** * Ends the activity of the specified Class */public void finishactivity (class<?> cls) {for (Activity activity:activitystack) {if (act Ivity.getclass (). EQuals (CLS)) {finishactivity (activity); return;}}}            /** * End all activity */public void finishallactivity () {for (int i = 0, size = activitystack.size (); i < size; i++) {            if (null! = Activitystack.get (i)) {activitystack.get (i). Finish (); }}activitystack.clear ();} /** * Exit application */public void AppExit (context context) {try {finishallactivity (); Android.app.ActivityManager activitymgr= ( Android.app.ActivityManager) Context.getsystemservice (Context.activity_service); Activitymgr.restartpackage ( Context.getpackagename ()); System.exit (0);} catch (Exception e) {}}}


==================================================================================================

Below from the http://www.oschina.net/code/snippet_191986_11730 to take a piece of code, encapsulates the activity full screen, hide the system title bar, set screen orientation, activity jumps and other functions. The code is as follows:


Import Java.util.Map; Import Org.apache.http.NameValuePair; Import Android.app.activity;import Android.content.intent;import Android.content.pm.activityinfo;import Android.view.window;import Android.view.windowmanager;import Android.widget.Toast; /** * </br><b>name: </b> activityutil * </br><b>description: </b>activity feature Assistant with * </br> @author: Under the bridge a grain of sand * </br><b>e-mail: </b> [email protected] * </br> <b>weibo: </b> @ bridge under a grain of sand * </br><b>date: </b> 2012-7-8 * */public class Activity Util {/** * </br><b>title: </b> Set Activity full Screen display * </br><b>description: &L     T;/b> Set activity full screen display. * @param activity Activity Reference * @param isfull true for fullscreen, false for non-fullscreen */public static void Setfullscreen (Activity act        Ivity,boolean isfull) {window window = Activity.getwindow (); Windowmanager.layoutparams params = WindOw.getattributes ();            if (isfull) {params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;            Window.setattributes (params);        Window.addflags (WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);            } else {params.flags &= (~windowmanager.layoutparams.flag_fullscreen);            Window.setattributes (params);        Window.clearflags (WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); }}/** * </br><b>title: </b> Hide System title bar * </br><b>description: </        B> Hidden Activity system default title bar * @param Activity Activity Object */public static void Hidetitlebar (activity activity) {    Activity.requestwindowfeature (Window.feature_no_title); }/** * </br><b>title: </b> set activity to display in a vertical direction * </br><b>descriptio     N:</b> forces the display direction of the actiity to be vertically oriented. * @param activity Activity Object */public static void Setscreenvertical (AcTivity activity) {activity.setrequestedorientation (activityinfo.screen_orientation_portrait); }/** * </br><b>title: </b> set Activity's display direction to landscape * </br><b>description     The:</b> force setting actiity is displayed in landscape orientation. * @param activity Activity Object */public static void Setscreenhorizontal (activity activity) {Activity.setreque    Stedorientation (Activityinfo.screen_orientation_landscape); }/** * </br><b>title: </b> Hide Software ime * </br><b>description:</b> Hidden Software Input Method * </br><b>time:</b> 2012-7-12 pm 7:20:00 * @param activity */public static V OID Hidesoftinput (activity activity) {Activity.getwindow (). Setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT    _state_always_hidden); }/** * </br><b>title: </b> make UI adaptable IME * </br><b>description: </b&gt ; Make UI adaptable IME * </br><b>time:</b> 2012-7-17 PM 10:21:26 * @param activity */public static void Adjustsoftinput (activity activity)    {Activity.getwindow (). Setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);     }/** * </br><b>title: </b> jump to an activity. * </br><b>description:</b> jump to an activity * </br><b>time:</b> 2012-7-8 3:20: XX * @param activity activity * @param targetactivity Target Activity class */public static void S Witchto (activity activity,class<? extends activity> targetactivity) {switchTo (activity, new Intent (activity,t    argetactivity)); }/** * </br><b>title: </b> activity Jump according to a given intent * </br><b>descrip tion:</b> activity Jump according to a given intent * </br><b>time:</b> 2012-7-8 pm 3:22:23 * @param Act Ivity Activity Object * @param inteNT Intent object to pass */public static void SwitchTo (Activity activity,intent Intent) {Activity.startac    Tivity (Intent); }/** * </br><b>title: </b> with parameters for activity jump * </br><b>description: &L T;/b> with parameters for activity jump * </br><b>time:</b> 2012-7-8 pm 3:24:54 * @param activity A Ctivity Object * @param targetactivity target Activity class * @param params jump parameter */public static V  OID SwitchTo (Activity activity,class<? extends activity> targetactivity,map<string,object> params) {if (            Null! = params) {Intent Intent = new Intent (activity,targetactivity); For (map.entry<string, object> entry:params.entrySet ()) {setvaluetointent (Intent, Entry.getkey (), EN            Try.getvalue ());        } switchTo (activity, intent); }}/** * </br><b>title: </b> with parameters for ACTIVity Jump * </br><b>description:</b> with parameters for activity jump * </br><b>time:</b> 201 2-7-17 pm 10:22:58 * @param activity * @param target * @param params */public static void SwitchTo (Acti Vity activity,class<? Extends activity> target,namevaluepair...params) {if (null! = params) {Intent Intent = new Intent (AC            Tivity,target);            for (Namevaluepair param:params) {setvaluetointent (Intent, Param.getname (), Param.getvalue ());        } switchTo (activity, intent);     }}/** * </br><b>title: </b> display toast message. * </br><b>description:</b> Displays toast messages and is guaranteed to run in the UI thread * </br><b>time:</b> 2012-7-1 0 pm 08:36:02 * @param activity * @param message * * public static void Toastshow (final activity Activity,fin            Al String message) {Activity.runonuithread (new Runnable () {public void Run () {Toast.maketext (activity, message, Toast.length_short). Show ();    }        }); }/** * </br><b>title: </b> Set the value to intent * </br><b>description: </      B> set the value to intent * </br><b>time:</b> 2012-7-8 pm 3:31:17 * @param intent Inent objects * @param key Key * @param val Value */public static void Setvaluetointent (Int        Ent Intent, String key, Object val) {if (val instanceof Boolean) Intent.putextra (Key, (Boolean) val);        else if (Val instanceof boolean[]) Intent.putextra (Key, (boolean[]) val);        else if (Val instanceof String) Intent.putextra (Key, (String) val);        else if (Val instanceof string[]) Intent.putextra (Key, (string[]) val);        else if (Val instanceof Integer) Intent.putextra (Key, (Integer) val); else if (Val instanceof inTeger[]) Intent.putextra (Key, (integer[]) val);        else if (Val instanceof Long) Intent.putextra (Key, (Long) val);        else if (Val instanceof long[]) Intent.putextra (Key, (long[]) val);        else if (Val instanceof Double) Intent.putextra (Key, (Double) val);        else if (Val instanceof double[]) Intent.putextra (Key, (double[]) val);        else if (Val instanceof Float) Intent.putextra (Key, (Float) val);    else if (Val instanceof float[]) Intent.putextra (Key, (float[]) val); }     }


==================================================================================================

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

==================================================================================================   


Copyright NOTICE: This article for Bo Master original article, welcome reprint, reprint please indicate source Http://blog.csdn.net/ouyang_peng

My Android Step-by-step tour------>android A simple package for activity management

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.