Activitiy Open a aactivitiy,aactivitiy will walk the following life cycle
oncreate– "onstart–" onresume– "a Run –" issue Open B's intent– "aonpause–" b Visible – "Aonstop, at this time B goes through the same life cycle,
When b ends, a is again visible, onrestart– "onstart–" Onresume
Note: After Aonpause B is visible, in order not to affect the display of the B interface, it is better not to take a time-consuming operation in the onpause of a, time-consuming operation can be done in OnPause, because B is now visible.
One, the life cycle in exceptional circumstances
1, the resource-related system configuration has changed
For example, the screen switch, the system resource configuration has changed, the activity will be destroyed and re-created, because it is an abnormal termination, the activity will use the Saveinstancestate method to save the state, this method before OnStop, with OnPause no timing relationship , when Activitiy is rebuilt, the system will pass the bundle objects saved by Saveinstancestate to the Onrestoreinstancestate and OnCreate methods,
2, low-priority activity is killed due to insufficient system memory
Activity priority
1, the activitiy with user interaction at the foreground is the highest priority,
2, visible but not central to the foreground, such as a pop-up dialog or display progress so that activity cannot interact with the user, but the activity is still visible
3, in the background loses focus, at this time activity in OnStop state, the lowest priority.
When the system resources are insufficient, priority is given to killing the low priority activity, because of the abnormal termination, the system will call Saveinstancesate and Onrestoreinstancestate two methods to save and restore the data
Second, the V Realization of the activity transition
1. Using the Overridependingtransition method to achieve activity jump animation
The Overridependingtransition method is the activity jump animation method provided in activity, which can realize the animation effect when the activity jumps.
Like what:
Intent Intent =newintent (Mainactivity.this, Secondactivity.class);
StartActivity (Intent);
Overridependingtransition (R.anim.slide_in_left, r.anim.slide_in_left);
This function has two parameters, one parameter is the animation when the first activity enters, and the other is the animation when the second activity exits.
Here's a special note about the Overridependingtransition function, which has two points that require an idea.
1> it must be called next to the StartActivity () or the finish () function.
2>: It only works on android2.0 and above
2. Use style to define switch animations for activity
(1) Define application's theme to define style for the theme
Android:allowBackup="true"Android:icon="@mipmap/ic_launcher"Android:label="@string/app_name"Android:supportsRtl="true"Android:theme="@style/AppTheme">
(2) Define the specific Apptheme style
The Windowanimationstyle here is the style where we define the activity toggle animation. And @anim/slide_in_top is our definition of the animation file, that is, by setting the style for Appliation, and then set the animation file for the Windowanimationstyle can be a global animation for the activity jump configuration.
<!--Base application theme.<stylename="Apptheme"Parent="Theme.AppCompat.Light.DarkActionBar"> <!--Customize your theme here.<Item name="Colorprimary"> @color/colorprimary</Item><Item name="Colorprimarydark"> @color/colorprimarydark</Item> <Item name="Coloraccent"> @color/coloraccent</Item><Item name="Android:windowanimationstyle"> @style/activityanim</Item>
</style><!-- 使用style方式定义activity切换动画 --> <style name="activityAnim"> <item name="Android:activityOpenEnterAnimation">@anim/slide_in_top</item> <item name="Android:activityOpenExitAnimation">@anim/slide_in_top</item> </style>
And there are four kinds of animations in Windowanimationstyle:
Activityopenenteranimation
Lets you set up an animation that opens a new activity and enters a new activity display
Activityopenexitanimation
Lets you set up animations that open new activity and destroy previous activity shows
Activitycloseenteranimation
Used to set the animation that closes the current activity into the previous activity display
Activitycloseexitanimation
Used to set the animation that is displayed when the current activity is closed
Only two animations were made in the case.
3. Use activityoptions Toggle animation to achieve activity jump animation
Through the Overridependingtransition method basically can satisfy our daily activity jump rotation picture demand, but after MD style comes out, overridependingtransition this old, How can the blunt way fit our MD-style app? Activityoptions is an over-animation of another activity that Google has given us in the new SDK. and the Compatibility Pack--activityoptionscompat is provided. Activityoptionscompat is a static class, which provides the corresponding activity jump animation effect, through which can achieve a lot of cool animation effect.
(1) Set contentfeature in jump activity
@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate); Set Contentfeature, you can use Toggle Animation GetWindow (). Requestfeature(Window. FEATURE_content_transitions); Transition explode = Transitioninflater. from(this). Inflatetransition(Android. R. Transition. Explode);GetWindow (). Setentertransition(explode); Setcontentview (R. Layout. Activity_three); }
(2) When StartActivity executes the jump logic, call StartActivity's override method, execute the Activityoptions.makescenetransitionanimation method
/** * 点击按钮,实现Activity的跳转操作 * 通过Android5.0及以上代码的方式实现activity的跳转动画 */button3.setOnClickListener(new@OverridepublicvoidonClicknew Intent(MainActivity.this, ThreeActivity.class); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle()); } });
4, use the activityoptions after the built-in animation effect through the style of the way
This is a way to show and use activityoptions over-animation in style, and here's how to define over-animation by defining a style:
(1) Writing excessive animation files
First we need to create a new transition directory under the application project Res directory, and then make a resource file, and then use the transition animation effect that comes with these systems, which sets an over-duration of 300ms.
xmlns:Android="http://schemas.Android.com/apk/res/Android"Android:duration="300" />
(2) Define the style file and refer to the resource defined in the translation directory
<!--Base application theme.<stylename="Apptheme"Parent="Theme.AppCompat.Light.DarkActionBar"> <!--Customize your theme here.<Item name="Colorprimary"> @color/colorprimary</Item><Item name="Colorprimarydark"> @color/colorprimarydark</Item> <Item name="Coloraccent"> @color/coloraccent</Item><Item name="Android:windowentertransition"> @transition/activity_explode</Item><Item name="Android:windowexittransition"> @transition/activity_explode</Item> </style>
Add in the application style file and specify that the transition animation effect is the transition animation file we just defined:
<itemname="Android:windowEnterTransition">@transition/activity_explode</item><itemname="Android:windowExitTransition">@transition/activity_explode</item>
(3) Perform jump logic
Click on the button to achieve the activity's jump action * to achieve activity's jump animation by Android5.0 and above style
button4.setOnClickListener(new@OverridepublicvoidonClick/** * 调用ActivityOptions.makeSceneTransitionAnimation实现过度动画 */new Intent(MainActivity.this, FourActivity.class); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle()); } });
(v) The use of Activityoptions animation to share components of the way to achieve jump activity animation
The shared component animation here refers to a transition between a child view of the previous activity and a sub-view of the next activity, that is, the jump action of the activity under this excessive effect. So how do you achieve transitions between two component view?
(1) Defining shared components
Click the Transitionname property in activity a button:
<Button Android: id="@+id/button5" Android: layout_width="Match_parent" Android: layout_height="Wrap_content" Android: layout_below="@+id/button4" Android: layout_margintop="10DP" Android: layout_marginright="10DP" Android: layout_marginleft="10DP" Android: text="component over-animation" Android: background="@color/colorprimary" Android: transitionname="Sharenames" />
Define the Transitionname property for the component in activity B's layout file so that the two components are equivalent to an over-correspondence, and it is important to note that the values of the Transitionname properties of the two components must be the same.
<?xml version="1.0"encoding="Utf-8"?><LinearLayout xmlns:Android="Http://schemas.Android.com/apk/res/Android" Android: id="@+id/activity_second" Android: layout_width="Match_parent" Android: layout_height="Match_parent" Android: gravity="Center_horizontal" Android: orientation="vertical" Android: transitionname="Sharenames" > <TextView Android: layout_width="Match_parent" Android: layout_height="Match_parent" Android: background="@color/coloraccent" Android: layout_margintop="10DP" Android: layout_marginbottom="10DP" /></linearlayout>
(2) Call StartActivity to perform a jump animation
Click the button to achieve the activity's jump action * Jump animation of activity by Android5.0 and above shared components
button5.setOnClickListener(new@OverridepublicvoidonClicknew Intent(MainActivity.this, FiveActivity.class); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this"shareNames").toBundle()); } });
What needs to be explained is the Activityoptions.makescenetransitionanimation method called here, passed three parameters, where the first parameter is the context object, the second parameter is the shared component that initiates the activity, and the third parameter is the start Activit The shared component of Y Transitionname the property value.
In this way, we implement the over-effect of components in a from activity A to activity B when we jump to the component in B.
Summary of Transition animations
The Overridependingtransition method starts from the Android2.0 and can basically cover the needs of our activity jumping and rotating painting;
Activityoptions API started in Android5.0, can achieve some cool animation effect, more in line with MD style, Activityoptions can also realize the excessive animation between two activity components;
The animation that defines the application style is a global animation, and a local animation is set in Java code
Reference articles
http://www.jianshu.com/p/c21216bf5f82
Http://www.cnblogs.com/bavariama/p/3368515.html
Android Learning activity in depth