Android Basics Getting Started tutorial--4.1.2 activity First Glimpse

Source: Internet
Author: User

Android Basics Getting Started tutorial--4.1.2 activity First Glimpse

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

In the previous section we learned about some of the basic concepts of activity, what is the life cycle of activity,activity, how to start an activity, and so on, this section we continue to study activity, The front also said that an app is often composed of multiple activity, which involves a number of data transfer between activity issues, then this section continue to learn the use of activity! In addition, we will talk about the transfer of collections, objects, arrays, and bitmap in intent, where only the basic data can be passed!

Data transfer between 1.Activity:

code example:

Code Download: Activitytest1.zip

2. Interaction between multiple activity (the latter one is passed back to the previous one)

code example:

Code Download: Activitytest2.zip

3. Know which activity is currently

4. Close all activity at any time

Sometimes we might open up a lot of activity and suddenly come up with one of those needs that can be turned off on a page
All activity and quit the program! Okay, here's a way to turn off all activity,
is to use a list collection to store all activity!

The specific code is as follows:

 Public classActivitycollector { Public Staticlinkedlist<activity> activities =NewLinkedlist<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 (); }          }      }  }
5. How to exit the app completely

It says close all activity, but sometimes we might want to kill the entire app, and even the background task will kill
If you kill them all, you can use them with the following code:

Implementation code:

/**  * 退出应用程序  */  publicvoidAppExit(Context context) {      try {          ActivityCollector.finishAll();          ActivityManager activityMgr = (ActivityManager) context                  .getSystemService(Context.ACTIVITY_SERVICE);          activityMgr.killBackgroundProcesses(context.getPackageName());          System.exit(0);      catch (Exception ignored) {}  }  
6. Two ways to double-exit the program: 1) Define a variable to identify whether to exit
//Define a variable to identify whether to exitPrivate Static BooleanIsexit =false; Handler Mhandler =NewHandler () {@Override     Public void Handlemessage(Message msg) {Super. Handlemessage (msg); Isexit =false; }}; Public Boolean OnKeyDown(intKeyCode, KeyEvent event) {if(keycode = = Keyevent.keycode_back) {if(!isexit) {Isexit =true; Toast.maketext (Getapplicationcontext (),"Press one more time to exit the program", Toast.length_short). Show ();//Use handler delay to send Change status informationMhandler.sendemptymessagedelayed (0, -); }Else{Exit ( This); }return false; }return Super. OnKeyDown (KeyCode, event);}
2) Save click Time:
//Save the time of the clickPrivate LongExittime =0; PublicBooleanOnKeyDown(intKeyCode, KeyEventEvent) {if(keycode = = Keyevent.keycode_back) {if((System.currenttimemillis ()-Exittime) > -) {Toast.maketext (Getapplicationcontext (),"Press one more time to exit the program", Toast.length_short). Show ();        Exittime = System.currenttimemillis (); }Else{exit (); }return false; }returnSuper.onkeydown (KeyCode,Event);}
7. Set the scene animation for the activity

The so-called cut-off animation is to switch to another activity when coupled with some switching animation, such as fading, zooming in and out, pushing around and so on!
Of course, we are not here to explain the animation in detail, there are special chapters to explain this, here only to teach you how to load the animation, in addition
To provide you with some of the more commonly used transition animation, as long as the relevant animation files are added to the Res/anim directory, and then the following method two select a
You can switch animations for your activity!

1) Method One:

2) Method Two:

With the style configuration, this is the global, oh, all the activity will load this animation!

The implementation code is as follows:

① custom Style in Style.xml:

<!--default activity jump rotate ----<style name="Default_animation" mce_bogus="1" parent ="@android: style/animation.activity"><item name= "android: activityopenenteranimation">@anim/ default_anim_in</item> <item name="android:activityopenexitanimation"> @anim/anim_stay </item> <item name="android:activitycloseenteranimation"> @anim/anim_stay</item> < Item name="Android:activitycloseexitanimation"> @anim/default_anim_out</item> </style>

Explain:

4 Item (each) represents a
Activity a jumps to activity B when activity B enters the animation;
Activity a jumps to activity B when activity a exits the animation;
Activity a enters animation when activity B returns to activity a
Activity b Returns the Activityb animation when activity A is returned

② then modify the following apptheme:

name="AppTheme" mce_bogus="1" parent="@android:style/Theme.Light">        <itemname="android:windowAnimationStyle">@style/default_animation</item>        <itemname="android:windowNoTitle">true</item></style>

③ finally under Appliction settings:

<application   android:icon="@drawable/logo"   android:label="@string/app_name"   android:theme="@style/AppTheme" >

OK, the animation effect is so Duang set up ~

3) Other

Well, in addition to the above two methods, you can also use Transitionmanager to achieve, but the requirements version is API 19 or more,
There is also a addonpredrawlistener conversion animation, this is still a bit cumbersome to use, may not be suitable for beginners
Here also does not speak, finally provides the next some commonly used animation effect packing, chooses the need special effects to join the project!
Transition animations commonly used in activity. zip

8.Bundle restrictions on passing data

When using bundles to pass data, be aware that the bundle size is limited < 0.5MB, if it is greater than this value
is to report transactiontoolargeexception abnormal!!!

9. Use the command line to view all current activity commands:

Use the following command, provided you configure environment variables for the SDK:adb shell Dumpsys activity

10. How to set Activity Full screen: 1) code-behind Actionbar

Call Getactionbar.hide () in the OnCreate method of the activity;

2) set by requestwindowfeature

Requestwindowfeature (Window.feature_no_title);
The code needs to be called before Setcontentview (), otherwise it will be an error!!!

3) through Androidmanifest.xml's theme

Set theme = @android in tags that require full-screen activity: Style/theme.notitlebar.fullscreen

The 11.onWindowFocusChanged method is magical:

Let's take a look at the official introduction to this method:

That is, when the activity gets or loses focus, the method is called back!
If we want to monitor if the activity is loaded, we can use this method.
For further information, you can go to this post:
Onwindowfocuschanged Trigger Introduction

12. Define dialog-style activity

In some cases, we may need to set activity to a dialog-style, where activity is generally full-screen,
And dialog is a part of the screen! The implementation is also very simple!

Directly set the theme of the activity:

android:theme="@android:style/Theme.Dialog"

So you can, of course, you can set the next title, Small icon!

//设置左上角小图标requestWindowFeature(Window.FEATURE_LEFT_ICON); setContentView(R.layout.main); getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_lion_icon);//设置文字:setTitle(R.string.actdialog_title);  //XML代码中设置:android:label="@string/activity_dialog"
This section summarizes:

OK, this section we have learned a bit about activity in the actual development of some common problems, I believe in the actual development will help everyone!
In the next section we will learn the concept of the activty stack, as well as four loading modes! Please look forward to ~ Thank you ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--4.1.2 activity First Glimpse

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.