Android: Implements switching between two activities without taking onCreate (). (The program is smoother !), Androidoncreate

Source: Internet
Author: User

Android: Implements switching between two activities without taking onCreate (). (The program is smoother !), Androidoncreate

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:

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="150"    android:fromXDelta="0"    android:toXDelta="-100%p"    android:fromYDelta="0"    android:toYDelta="0" ></translate>

(2) The Activity exits from the right side:

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="150"    android:fromXDelta="0"    android:fromYDelta="0"    android:toXDelta="100%p"    android:toYDelta="0" ></translate>

(3) Activity enters from the left:

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="150"    android:fromXDelta="-100%p"    android:fromYDelta="0"    android:toXDelta="0"    android:toYDelta="0" ></translate>

(4) Activity enters from the right:

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="150"    android:fromXDelta="100%p"    android:toXDelta="0"    android:fromYDelta="0"    android:toYDelta="0" ></translate>

This article is complete.

In the next article, we will use this method to implement the QQ photo selection function.




How to Implement android return to the previous Activity and re-execute the onCreate Method

When the first activity jumps to the second activity, finish () The first activity. In this way, when the second activity returns the first activity, the onCreate () method of the first activity is executed.

Which of the following programs is wrong? I want to implement conversion between the MAIN and OTHER activities.

. You do not need to start ac1.java, but choose to start ac2.java. In this case, you can change the startup class in AndroidManifest. xml. If the class name of ac2.java is TwoActivity,
Change (class name...) to TwoActivity.
<Activity
Android: name = ". (class name ..)"
Android: label = "@ string/app_name"
>
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>

2. You need to start both ac1.java and ac2.java. I don't think you can start ac1.java at the same time. After all, you have a single screen and it is impossible to display the content of two screens. Unless you have a choice, ac1.java is displayed in the first case; ac2.java is displayed in the second case. However, one problem exists. This if statement is written there, and xml cannot be written, so it can only be written in the java file. That is to say, the codes of ac1.java and ac2.java should be combined and written in A. java file.
You can create a new Activity. You have two layout XML files one. xml and two. xml. In the onCreate method of this Activity
If (Case 1 ){
SetContentView (R. layout. one );
} Else (Case 2 ){
SetContentView (R. layout. two );
}
In this way, you can select two la S. in Case 1, initialize the variables in Case 1, and in Case 2, initialize the variables in Case 2, and write them separately.

3. Start two files sequentially, jump from ac1.java to ac2.java, and use startActivity (intent) in ac1.java to jump.
In addition, group purchases on Virtual Machine groups are extremely cheap.

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.