problem
Usually you are used to startActivities. When someone asks you what startActivities mean, you might ask them to start multiple activities at the same time. It's just a question. After verification, you find it's not as simple as you think.
Prepare three ActivityA,ActivityB,ActivityC:
Prepare code:
fun multiIntent(view : View) { val intent_1 = Intent(this,IntentActivityA::class.java) val intent_2 = Intent(this,IntentActivityB::class.java) val intent_3 = Intent(this,IntentActivityC::class.java) startActivities(arrayOf(intent_1,intent_2,intent_3)) }
The results are shown as follows:
As you can see, the first display is ActivityC, then ActivityB, and finally ActivityA, is this the case: When I startActivities, ActivityA, ActivityB, and ActivityC are all created, but pushed into the bottom of the stack one by one, I finish ed the ActivityC, cleared the top of the stack, then ActivityB is visible, after clearing the ActivityB, ActivityA is at the top of the stack?
Or another scenario: Create ActivityC first, wait until ActivityC finish es fall, then start creating ActivityB, wait until ActivityB finish es fall, then create ActivityA?Let's start with a log:
IntentActivity C onCreate IntentActivity B onCreate IntentActivity C onDestroy IntentActivity A onCreate IntentActivity B onDestroy IntentActivity A onDestroy
From the log, we find that the logic of startActivities is our way two, created by a single Activity, and wait until the Activity finish es before creating the remaining Activities; it's not what we expected to create all at once.
application
With all this said, will you feel that this item is useless, because I don't meet many of these requirements?Indeed, I didn't know about this method before and I've never used it before, but I've recently encountered such a problem. First, take a look at the Open Source China explanation:
What is the main problem with this program?He clicked on the details of the advertisement to get to the target page, then clicked back. Instead of going back to the main interface as we expected, the whole program quit and then delayed entering the main interface. It didn't feel very good (I don't deny that the app in Open Source China is well written, and I learned a lot of good things) because we also had good projects.This problem arises, and the flowchart is probably like this:
The app logic is different for each house. We use this to show an ad page on the front page (as shown in the Open Source China OSC), then you click in to buy something and buy MainActivity which goes directly to the main interface.At the beginning, we did spicy chicken with the following pseudocode:
# In ProductDetailActivity # The return button and the back button share the same method public void onBackPress() { if(isFromSplashPage) { //If you've come from the Splash page, you need to go back to the main interface here MainActivity.start(this); finish(); }else { //Other pages can enter the product details page and exit the page directly at this time. finish(); }
That's not impossible, but once you realize what startActiitives does, you don't need to pass in the isFromSplashPage parameter, and you can do it:
# SplashActivity Page var intent_main = Intent(this, MainActivity::class.java) var intent_product = Intent(this, ProductDetailActivity::class.java) startActivities(arrayOf(intent_main, intent_product))
That's not the point yet, it's the discovery that when I find out that our product is being pushed, I can also use this logic:
If your login is successful, I can show you the push details page directly, otherwise I can sign you in directly, and the pseudocode can be as follows:
# PushUtils: if(userHasLogin) { var intent_detail = Intent(this, PushDetailActivity::class.java) var intent_main = Intent(this,MainActivity::class.java) startActivitied(arrayOf(intent_detail, intent_main)) }else { startActivity(Intent(this,LoginActivity::class.java)) } #LoginActivity if(LoginSuccess) { var intent_detail = Intent(this, PushDetailActivity::class.java) var intent_main = Intent(this,MainActivity::class.java) startActivitied(arrayOf(intent_detail, intent_main)) }else{ var intent_main = Intent(this,MainActivity::class.java) startActivity(intent_main) finish() }
Okay, let's sum it up. The next time you encounter this kind of problem, you will know how to write it.