Android programming enables two activities to switch to each other without using the oncreate() method

This example describes the method of Android programming to switch between two activities without oncreate (). Share with you for your reference, as follows:

The objectives to be achieved here are:

There are three activities: A, B and C. you can enter B from a and C from B. you may need to switch between B and C for many times. Therefore, you can't use the normal startactivity finish method because it takes time to re create an activity. When there are many contents in this activity, repeated creation greatly affects the fluency. (that is, it is similar to selecting photo page - selecting album page on QQ. These two pages may switch to each other many times, so they cannot be created many times)

1. When entering C from B or B from C, use startactivity (not finish) and add the parameter flag for intent_ ACTIVITY_ REORDER_ TO_ Front, i.e.:

In this case, if C has not been created before, it will be created. If it has been created, it will only move C from the acity stack to the top of B without re creation.

2. Rewrite the onnewintent method in C, that is:

The parameter intent in this method is the intent of startactivity from B to C. when entering C for the second time, the page of C can be updated according to the data transmitted from B in this method. (when entering C for the first time, go oncreate instead of onnewintent)

3. Also rewrite onnewintent in B, which is the same as 2 above.

4. When you use a button or return key in a and B to return to the first page a, you also use startactivity and add the parameter flag for intent_ ACTIVITY_ CLEAR_ Top, so that both B and C will be closed (finish)

5. Similarly, rewrite the onnewintent method in a to process the information passed when B or C returns. Also, add the attribute for a in the manifest

6. Last question, since startactivity is used from B to C and from C to B, the animation switched between activities is the style of starting a new activity. For example, no matter from B to C or from C to B, the new page enters from the right, which gives people the feeling that they have been creating a new activity, which is very unfriendly.

The way to solve this problem is to customize the animation of switching between activities. The purpose is to give users a feeling: from C to B, it is a new activity (b enters from the right), and from B to C, it is "back" (c enters from the left). Just one line of code:

In: animation of the entered activity; Out: the animation of the exited activity.

Note here:

(1) For the normal startactivity finish mode, if you need to customize the animation of switching between activities, you only need to write the above code after startactivity or finish: (2) however, due to the use of flag in this article_ ACTIVITY_ REORDER_ TO_ In front mode, the above animation will only be effective 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 entry style of C and the exit style of B.

7. Animation resources:

(1) Activity exits from the left:

(2) Activity exits from the right:

(3) Activity enters from the left:

(4) Activity enters from the right:

For more Android related content, readers who are interested can view the topics on this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solutions, summary of Android multimedia operation skills (audio, video, recording, etc.), summary of Android basic component usage, summary of Android view skills Android layout skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>