当前位置:网站首页>How many of the 36 difficult points of activity do you know?, Android interview 2020

How many of the 36 difficult points of activity do you know?, Android interview 2020

2022-06-24 00:18:00 Ordinary netizens

Preface

  • learn Android It's been a while , Always focused on learning new things , Recently I found that a lot of things that are seldom used are forgotten , Take advantage of these two days , I'm going to Activity In the form of questions , It's also for you to check the defects and make up the omissions .

In this paper , I will change my habit of blogging , Full text XMind Present all knowledge points in the form of mind map , Welcome to eat ~~

List of articles


[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-QHgA2dEJ-1637978600673)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab263bcee1cb?imageView2/0/w/1280/h/960/ignore-error/1)]

Convenient for everyone to learn , I am here GitHub Create a Warehouse


Divine plan


  • Before we start , Let's have a look at Android Of activity What are the things ?
  • Borrow a very popular picture on the Internet to show you Activity

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-JiZh91hf-1637978600718)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab263d2eccd9?imageView2/0/w/1280/h/960/ignore-error/1)]

One 、 Life cycle


  • Post a famous picture first
  • Let's first look at the specific method callbacks in the life cycle , One by one :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-cD9Ev7RH-1637978600719)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab263d483f3c?imageView2/0/w/1280/h/960/format/png/ignore-error/1)]

1.1 Dialog When it pops up

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-17SS6rVL-1637978600720)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2640bfa92b?imageView2/0/w/1280/h/960/ignore-error/1)]

  • If it is simply created dialog ,Activity Methods that do not perform the lifecycle
  • But if it's a jump to a not full screen Activity Words , Of course, it is carried out according to the normal life cycle
  • namely onPasue() -> onPause() ( The original will not be executed Activity Of onStop() , Otherwise, the previous page will not display )

1.2 When switching between horizontal and vertical screens

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-dmKaWmyK-1637978600721)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2640d09989?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Not set up Activity Of android:configChanges when , Cutting the screen will recall each life cycle , It will be executed once when cutting the horizontal screen , When cutting the vertical screen, it will execute twice

  • Set up Activity Of android:configChanges="orientation" when , Cutting the screen will still recall each life cycle , Cut across 、 It will only be executed once when the screen is up

  • Set up Activity Of android:configChanges="orientation|keyboardHidden" when , Cutting the screen will not recall each life cycle , Only execute onConfigurationChanged Method

  • Be careful : And a little bit more , It's very important , One Android Details of changes ! When API >12 when , Need to add screenSize attribute , Otherwise, the screen will switch even if you set orientation The system will be rebuilt Activity

  • Execution of life cycle of horizontal and vertical screen switching

1.3 In different scenes Activity The changing process of life cycle

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-jEGNXCqT-1637978600722)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2662135b32?imageView2/0/w/1280/h/960/ignore-error/1)]

  • start-up ActivityonCreate() —> onStart() —> onResume() ,Activity Enter the running state .
  • When locking the screen, it will execute onPause() and onStop() , When you open the screen, you should execute onStart()onResume()

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-wxXTpsJv-1637978600723)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2662bc3f51?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Activity Back office : At present Activity Go to new Activity Interface or press Home Key back to main screen : onPause() —> onStop() , Enter stagnation state .
  • Activity Back to the front desk : onRestart() —> onStart() —> onResume() , Return to operation again .
  • Activity Back office : And the system is out of memory , The system will kill the background state Activity , If we go back to this Activity , Then I will go onCreate() --> onStart() —> onResume()

1.4 Will a Activity Set to window style

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-UmROzvvo-1637978600723)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2666b5baec?imageView2/0/w/1280/h/960/ignore-error/1)]

Just for us Activity Configure the following properties . android:theme="@android:style/Theme.Dialog"

1.5 Exit called multiple Activity Of Application

  • Usually, the user exits a Activity Just press the back key , We write code to exit activity Call directly finish() The method will do .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-mRyAqNdm-1637978600724)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2668399456?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Send specific broadcast :
  1. When you need to end the app , Send a specific broadcast , Every Activity After receiving the broadcast , close that will do .
  2. To someone activity Intention to register for broadcast registerReceiver(receiver, filter)
  3. If you've ever received close activity Broadcast of activity finish() fall
  • Recursive exit
  1. Just call finish() Method Put the current Activity sign out
  2. Opening new Activity When using startActivityForResult , And then add your own logo , stay onActivityResult In dealing with , Recursive close .
  • Actually
  1. It can also be done through intent Of flag To achieve intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) Activate a new activity.
  2. At this time, if the task stack already has the Activity , So the system will Activity All of the above Activity kill .
  3. In fact, it's equivalent to giving Activity The configured startup mode is singleTask .
  • Record open Activity
  1. Every time you open one Activity , Just record it .
  2. When you need to exit , Close each one Activity

1.6 Lock screen and unlock screen ,Activity How to execute a lifecycle

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-aMOQpERw-1637978600724)(https://user-gold-cdn.xitu.io/2019/10/30/16e1b5f0ff6a9d15?imageView2/0/w/1280/h/960/ignore-error/1)]

  • When locking the screen, it will execute onPause() and onStop() , When you open the screen, you should execute onStart()onResume()

1.7 modify Activity Enter and exit animation

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-sE06Pvt6-1637978600725)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2666eae897?imageView2/0/w/1280/h/960/ignore-error/1)]

  • There are two ways , First, by definition Activity The theme of , The second is through rewriting Activity Of overridePendingTransition Method .
  • By setting the theme style in styles.xml Edit code in , add to themes.xml file : stay AndroidManifest.xml Specified in Activity Appoint theme.
  • overwrite overridePendingTransition Method :overridePendingTransition(R.anim.fade, R.anim.hold);

1.8 Activity Four states

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-q3bp1il7-1637978600725)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26844b8ee7?imageView2/0/w/1280/h/960/ignore-error/1)]

  • runnig : Users can click ,activity On top of stack .
  • pausedactivity When you lose focus , By a non full screen activity To occupy or be possessed by a transparent activity Cover , In this state activity Not destroyed , All of its state information and member variables still exist , Just can't be clicked .( Memory crunch , This activity Could be recycled )

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-59AoZD9z-1637978600726)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2687e2a13a?imageView2/0/w/1280/h/960/ignore-error/1)]

  • stopped : This activity By another activity Full coverage , But this activity All state information and member variables for are still present ( Except for the memory crunch )
  • killed : This activity Has been destroyed , All of its state information and member variables no longer exist .

1.9 How to handle exception exit

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-AUgNWLqU-1637978600726)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab268bcbf377?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Activity When you exit abnormally --> onPause() --> onSaveInstanceState() --> onStop() --> onDestory()
  • It should be noted that onSaveInstanceState() Methods and onPause There's no strict sequence , Are likely to onPause Before , It is also possible to call after it , But it will be there. onStop() Call before method
  • Restart the Activity --> onCreate() --> onStart() --> onRestoreInstanceState() --> onResume()

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-EibX9emP-1637978600727)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab268c50ae91?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Understand the execution of this life cycle, and then you can answer , First of all, you need to know what the interviewer means : Is to restart and restore this Activity Or just quit the whole thing app
  • If you want to recover, you need to onSaveInstanceState() To save the data in onRestoreInstanceState() Recovery in
  • If you want to exit app So we need to capture the global exception information , And exit app
  • Of course, the personal suggestion is to use UncaughtExceotionHandler To catch global exceptions and exit app The operation of , This will reduce the legacy of previous crashes !

1.10 What is? onNewIntent

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-62q7F7Ii-1637978600727)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26900d9292?imageView2/0/w/1280/h/960/ignore-error/1)]

  • If IntentActivity At the top of the task stack , That is to say, it was opened before Activity , Now in onPauseonStop In terms of state , Resend other apps Intent Words

  • The order of execution is :onNewIntent,onRestart,onStart,onResume.

Two 、 Boot mode


2.1 Boot mode

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-DhpcFeNY-1637978600728)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2695ac3863?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Activity There are four launchModestandardsingleTopsingleTasksingleInstance .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-oec1MGe6-1637978600728)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26af18ff50?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Standard Pattern ( The default mode )
  1. explain : One at a time Activity Will create a new instance to be pushed again , Whether this instance exists or not .

  2. Life cycle : Every instance created Activity 's life cycle is typical , its onCreateonStartonResume Will be called .

  3. give an example : here Activity There are ABC Three Activity , here C At the top of the stack , The startup mode is Standard Pattern . If in C Activity Add click event to , You need to jump to the same type of C Activity . It turns out there's another one C Activity Enter the stack , Become the top of the stack .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-aflIXLrW-1637978600729)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26af2f73ef?imageView2/0/w/1280/h/960/ignore-error/1)]

  • SingleTop Pattern ( Stack top reuse mode )
  1. explain : There are two ways to deal with it : What needs to be created Activity When already at the top of the stack , At this time, the top of the stack will be reused directly Activity . No more new Activity ; To create Activity Not at the top of the stack , A new one will be created again Activity Push , Same as Standard Model as .

  2. Life cycle : In case I Activity When directly reused , its onCreateonStart Not called by the system , Because it didn't change . But a new way onNewIntent Will be recalled ( Activity This method will not be called back when it is created normally ).

  3. give an example : here Activity There are ABC Three Activity , here C At the top of the stack , The startup mode is SingleTop Pattern . Situation 1 : stay C Activity Add click event to , You need to jump to the same type of C Activity . The result is a direct reuse of the top of the stack C Activity. Situation two : stay C Activity Add click event to , You need to jump to another one A Activity. The result is a new Activity Push . Become the top of the stack .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-VPhueTLm-1637978600729)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26b234a4cb?imageView2/0/w/1280/h/960/ignore-error/1)]

  • SingleTask Pattern ( In stack reuse mode )
  1. explain : To create Activity When already on the stack , New will not be created at this time Activity , It's going to be on the stack Activity The others above Activity All destruction , Make it the top of the stack .

  2. If you start it in another application , Then a new task , And in time task Start this Activity ,singleTask Allow something else Activity It's not in one task Co existing in , in other words , If I were here singleTask Open a new Activity , This new Activity Still will be in singleTask Instance task in .

  3. Life cycle : Same as SingleTop The same in pattern one . Just another callback Activity Medium onNewIntent Method

  4. give an example : here Activity There are ABC Three Activity . here C At the top of the stack , The startup mode is SingleTask Pattern . Situation 1 : stay C Activity Add click event to , You need to jump to the same type of C Activity . The result is a direct use of the top of the stack C Activity . Situation two : stay C Activity Add click event to , You need to jump to another one A Activity . The result is that A Activity above BC All destruction , send A Activity Become the top of the stack .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Ra1krKIV-1637978600729)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26b197fba4?imageView2/0/w/1280/h/960/ignore-error/1)]

  • SingleInstance Pattern ( Single instance mode )
  1. explain : SingleInstance A special , It's global singleton mode , It's a kind of reinforcement SingleTask Pattern . It has all its features , A little bit stronger : There is only one example , And this instance runs independently in a task in , This task This is the only example , Nothing else is allowed Activity There is .

  2. This is often used in system applications , such as Launch 、 Application of lock screen key, etc , There is only one in the whole system ! So in our application, we usually don't use . Just know .

  3. give an example : example A Activity It's the mode , start-up A after . The system will create a separate task stack for it , Due to the characteristics of stack reuse . No new requests will be created Activity , Unless this unique task stack is destroyed by the system .

2.2 How to use startup mode

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-LGm9ep9A-1637978600730)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26b281455b?imageView2/0/w/1280/h/960/ignore-error/1)]

  • stay Manifest.xml It is specified in Activity Boot mode
  1. A static specifying method
  2. stay Manifest.xml The document states Activity At the same time, specify its startup mode
  3. In this way, when you jump in the code, you will create... According to the specified pattern Activity .
  • start-up Activity when . stay Intent To create Activity
  1. A dynamic startup mode
  2. stay new One Intent after
  3. adopt Intent Of addFlags Method to dynamically specify a startup mode .
  • Be careful : Both of the above methods can be Activity Specify startup mode , But there is a difference between them .
  1. priority : Dynamic assignment means that the other priority is higher than the first one , If both exist at the same time , In another way .

  2. Limit the scope : The first method cannot be Activity Direct designation FLAG_ACTIVITY_CLEAR_TOP identification , The other way is not to Activity Appoint singleInstance Pattern .

2.3 Practical application scenario of startup mode

Of the four modes Standard Pattern is the most common , Nothing special . and SingleInstance Mode is a singleton mode of the whole system , In our application, it will not be applied to . therefore , Here is the specific explanation SingleTop and SingleTask Application scenario of the mode :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-rpWNWrIi-1637978600730)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26b65bb92a?imageView2/0/w/1280/h/960/ignore-error/1)]

  • SingleTask Application scenario of the mode
  1. The most common application scenario is to keep only one of our applications open Activity Example .
  2. The most typical example is the home page displayed in the application ( Home page ).
  3. Suppose the user jumps to another page on the home page , Want to return to the home page after running multiple operations , Suppose you don't use SingleTask Pattern , In the process of clicking back, you will see the home page many times , This is obviously the design is unreasonable .
  • SingleTop Application scenario of the mode
  1. Suppose you are in the current Activity And start the same type of Activity
  2. This type is recommended Activity The startup mode of is specified as SingleTop , Can reduce Activity The creation of , Save memory !
  • Be careful : Reuse Activity Life cycle callback for
  1. There's one more thing to consider here Activity The problem of carrying page parameters in jump .
  2. Because when a Activity Set up SingleTop perhaps SingleTask After the model , Jump here Activity Reuse of existing Activity situations , this Activity Of onCreate Method will not run again .onCreate Method will only be created for the first time Activity Run on .
  3. In general onCreate Method will initialize the data of this page 、UI initialization , Suppose the display data of the page is independent of the parameters passed by the page Jump , Don't worry about it
  4. If the data displayed on the page is through getInten() Method to get , Then problems will arise :getInten() It's always old data , There is no way to receive the new data transmitted during the jump !
  • following , Explain it with an example :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-mloWwTV9-1637978600731)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26cff841e2?imageView2/0/w/1280/h/960/ignore-error/1)]

  • In the above code CourseDetailActivity The boot mode set in the configuration file is SingleTop Pattern , According to the introduction of startup mode above , When CourseDetailActivity At the top of the stack .

  • Jump to page again CourseDetailActivity It will directly reuse the original Activity , And the data to be displayed on this page is from getIntent() The way to do it , But initData() Method will not be called again , At this point, the page cannot display new data .

  • Of course, this kind of situation system has been thought of for us for a long time , We need another callback onNewIntent(Intent intent) Method . This method will pass in the latest intent , In this way, we can solve the above problems . The recommended way here is to go again setIntent . And then again to initialize the data and UI . The code is as follows :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-vUjgTBf2-1637978600731)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26f4794839?imageView2/0/w/1280/h/960/ignore-error/1)]

  • such , In a page can repeatedly jump and display different content .

2.4 Quick start one Activity

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-YRasJQIq-1637978600732)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26f46155c8?imageView2/0/w/1280/h/960/ignore-error/1)]

  • In fact, this problem is relatively simple , Just don't Activity Of onCreate Too many heavy operations in method , And in onPasue Method also can't do too much time-consuming operation .

2.5 Start process

2.6 Activity Of Flags

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-WODqL5Bk-1637978600732)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26f431a604?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Marker bit can be set Activity Start mode of , As mentioned above , Specify the startup mode dynamically , example FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_SINGLE_TOP etc. . It can also affect Activity Operating state , example FLAG_ACTIVITY_CLEAN_TOP and FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS etc. .

  • Here are a few basic marker bits , Don't memorize , Just understand a few , Check official documents when necessary .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-zTLqJ8V1-1637978600732)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab26f64ed138?imageView2/0/w/1280/h/960/ignore-error/1)]

  • FLAG_ACTIVITY_NEW_TASK
  1. The purpose is to Activity Appoint “SingleTask” Boot mode . With the AndroidMainfest.xml The specified effect is the same
  • FLAG_ACTIVITY_SINGLE_TOP
  1. The purpose is to Activity Appoint “SingleTop” Boot mode , With the AndroidMainfest.xml The specified effect is the same .
  • FLAG_ACTIVITY_CLEAN_TOP
  1. With this flag bit Activity , At startup, the Activity Others in the same task stack Activity Out of the stack .
  2. Generally speaking, it is related to SingleTask Start mode appears together .
  3. It will be over SingleTask The role of .
  4. But in fact SingleTask The startup mode has the function of this flag bit by default
  • FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
  1. With this flag bit Activity Not in the present Activity List of
  2. Use scenarios : In some cases, we don't want users to go back to Activity when , This marker bit shows its effect .
  3. It is equivalent to xml It is specified in Activity Properties of .

2.7 onNewInstent() Method when to execute

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-o3JoQc5u-1637978600733)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2713491d3c?imageView2/0/w/1280/h/960/ignore-error/1)]

This is in startup mode , When this Activity There are already instances of , And the startup mode is SingleTask and SingleInstance , In addition, when this instance is at the top of the stack and the startup mode is SingleTop Can also be triggered when onNewInstent() .

3、 ... and 、 data


3.1 Activity Interpass Intent Transfer data size limit

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-1KYhCEzK-1637978600733)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2718c279eb?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Intent There is a size limit when passing data , Official didn't elaborate here , However, experimental methods can be used to measure the data should be limited to 1MB within ( 1024KB
  • We use delivery Bitmap Methods , Found when image size exceeds 1024( To be exact 1020 about ) When , The program will flash back 、 Stop operation and other exceptions ( Different mobile phones react differently )
  • So we can judge Intent The transmission capacity of 1MB within .

3.2 When there is not enough memory, the system will kill the background Activity, If you need to save some temporary state , In which way

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-krAg2jOQ-1637978600734)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab271cea5c35?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Activity Of onSaveInstanceState() and onRestoreInstanceState() It's not a life cycle approach , They are different from onCreate()onPause() Equal life cycle approach , They don't have to be triggered .

  • onSaveInstanceState() Method , When the application encounters an unexpected situation ( Such as : Out of memory 、 Users press directly Home key ) Destroy one by the system Activity ,onSaveInstanceState() Will be called .

  • But when the user takes the initiative to destroy one Activity when , For example, press the return key in the application ,onSaveInstanceState() It won't be called .

  • Unless it's time to activity It was not destroyed by the user , Usually onSaveInstanceState() It's only suitable for keeping some temporary state , and onPause() Suitable for data persistence .

3.3 onSaveInstanceState() Executed scenarios

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-JtOVIZRs-1637978600734)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2734eb5c0d?imageView2/0/w/1280/h/960/ignore-error/1)]

  • The system doesn't know you press HOME How many other programs to run after , Naturally, I don't know activity A Will it be destroyed
  • So the system will call onSaveInstanceState() , Give users the opportunity to save some non permanent data . The analysis of the following situations follows this principle :
  1. When the user presses HOME Key time
  2. Long press HOME key , When you choose to run another program
  3. When the screen is locked
  4. from activity A Start a new activity when
  5. Screen direction switching

3.4 Two Activity A method that must be executed when jumping between

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-ORzBiPHc-1637978600734)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2729e02a24?imageView2/0/w/1280/h/960/ignore-error/1)]

In general, for example, there are two activity , Respectively called A , B , When in A Activate inside B Component time , A Would call onPause() Method , then B call onCreate() , onStart() , onResume() .

This is the time B Form overridden , A Would call onStop() Method . If B It's transparent , perhaps Is the style of the dialog , Will not call A Of onStop() Method .

3.5 use Intent Go start a Activity Other methods

《Android Summary of learning notes + Latest mobile architecture video + Big Android interview questions + Project actual combat source code handout 》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 Full content open source sharing


[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-dOh141zb-1637978600735)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2724c3da3d?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Use adb shell am command
  1. am Start a activity
  2. adb shell am start com.example.fuchenxuan/.MainActivity
  3. am Send a broadcast , Use action
  4. adb shell am broadcast -a magcomm.action.TOUCH_LETTER

3.6 scheme Jump agreement

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-ZRriHc1N-1637978600735)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab273d2660f7?imageView2/0/w/1280/h/960/ignore-error/1)]

3.6.1 Definition

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-1j53T9WG-1637978600736)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab273f2b0b6b?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Server can customize jump app page

  • app Can pass Scheme Jump to another app page

  • Can pass h5 Page Jump app Native PAGE

3.6.2 Form of agreement :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-L8tEOveN-1637978600736)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab27484aabff?imageView2/0/w/1280/h/960/ignore-error/1)]

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-P4R7eG46-1637978600737)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab275b9808fe?imageView2/0/w/1280/h/960/ignore-error/1)]

  • qh representative Scheme Name of agreement

  • test representative Scheme Address field of scope

  • 8080 Represents the port number of the path change

  • /goods Represents the specified page ( route )

  • goodsId and name Represents the two parameters passed

3.6.3 Scheme Use

  • Define a Scheme

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-vKtLKAwG-1637978600737)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2743ad29d1?imageView2/0/w/1280/h/960/ignore-error/1)]

  • obtain Scheme Jump parameters

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-ev3aUWEj-1637978600738)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2765f5a51a?imageView2/0/w/1280/h/960/ignore-error/1)]

  • Call mode
  1. Native call

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-DEQOQ2dO-1637978600738)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2766001c6d?imageView2/0/w/1280/h/960/ignore-error/1)]

  1. html call

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-tqO8XRI3-1637978600738)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab276d16ed37?imageView2/0/w/1280/h/960/ignore-error/1)]

  1. Judge a certain Scheme Whether it works

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-4Qf9Y8Cw-1637978600739)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab278265ad01?imageView2/0/w/1280/h/960/ignore-error/1)]

Four 、Context


4.1 Context , Activity , Appliction The difference between

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Z9sbiCL8-1637978600739)(https://user-gold-cdn.xitu.io/2019/10/30/16e1ab2780c638c9?imageView2/0/w/1280/h/960/ignore-error/1)]
Jump parameters

[ Outside the chain picture transfer in …(img-ev3aUWEj-1637978600738)]

  • Call mode
  1. Native call

[ Outside the chain picture transfer in …(img-DEQOQ2dO-1637978600738)]

  1. html call

[ Outside the chain picture transfer in …(img-tqO8XRI3-1637978600738)]

  1. Judge a certain Scheme Whether it works

[ Outside the chain picture transfer in …(img-4Qf9Y8Cw-1637978600739)]

Four 、Context


4.1 Context , Activity , Appliction The difference between

[ Outside the chain picture transfer in …(img-Z9sbiCL8-1637978600739)]

原网站

版权声明
本文为[Ordinary netizens]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206232147195258.html

随机推荐