Android: Implements switching between two activities without taking onCreate (). (The program is smoother !)
The purpose of this article is:
There are three activities: A, B, and C. From A, you can enter B and B, and B and C may need to switch between each other multiple times, therefore, you cannot use the startActivity-finish method because it is time consuming to recreate the Activity. When this Activity contains a large amount of content, repeated creation greatly affects fluency. (This is similar to selecting a photo page on QQ-selecting an album page. These two pages may be switched to each other multiple times, so they cannot be created multiple times)
1. When B enters C or C enters B, startActivity is used (not finish), and FLAG_ACTIVITY_REORDER_TO_FRONT is added to intent, that is:
Intent intent = new Intent(B.this, C.class);intent.addFlags(Intent.FLAG_ACTIVITY__TO_FRONT);startActivity(intent);
In this case, if C has not been created before, it will be created. If it has already been created, it will only move C from the Aciticy stack to B, instead of re-creating.
2. Rewrite the onNewIntent method in C, that is:
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); }The intent parameter in this method is the intent of startActivity from B to C. When you enter C for the second time, you can update the page of C based on the data transmitted from B in this method. (OnCreate is used when you enter C for the first time, and onNewIntent is not used)
3. Rewrite onNewIntent in B, which is the same as above 2.
4. when you use A button or return key in A and B to return to the initial Page A, you also use startActivity and add the FLAG_ACTIVITY_CLEAR_TOP parameter to the intent. In this way, both B and C are closed (finish)
Intent intent = new Intent(this, A.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);
5. Similarly, rewrite the onNewIntent method in a to process the information transmitted when B or C returns. In addition, add the android: launchMode = "singleTop" attribute for A in Manifest"
6. In the last question, because startActivity is used for both B and C and B, the animation for switching between activities is to enable the style of the new Activity. For example, whether from B to C or from C to B, the new page is displayed on the right. It seems unfriendly to create a new Activity.
The method to solve this problem is to customize the animation for switching between activities. The purpose is to give the user a feeling that the Activity is newly opened (B enters from the right side) from C to B ), from B to C, it is "Returned" (C enters from the left ). Only one line of code is required:
overridePendingTransition(in, out);
In: Enter the animation of the Activity; out: exit the animation of the Activity.
Note:
(1) For normal startActivity-finish mode, if you need to customize the animation for switching between activities, you only need to write the above Code after startActivity or finish:
(2) However, because the FLAG_ACTIVITY_REORDER_TO_FRONT method is used in this article, the above animation will only be valid when page B or C is created for the first time. The solution is to write the above Code in onNewIntent. For example, from B to C, write this line of code in onNewIntent in C to specify the style C enters and B exits.
7. animation resources:
(1) The Activity exits from the left:
(2) The Activity exits from the right side:
(3) Activity enters from the left:
(4) Activity enters from the right:
This article is complete.
In the next article, we will use this method to implement the QQ photo selection function.