current position:Home>Android Jetpack component ViewModel
Android Jetpack component ViewModel
2023-01-25 11:31:10【Cloud valley area people】
ViewModel 概览
ViewModel概览https://developer.android.google.cn/topic/libraries/architecture/viewmodel
ViewModel Class is to pay attention to the life cycle of way to store and manage relevant data interface.ViewModel 类让数据可在发生屏幕旋转等配置更改后继续留存.
ViewModel的亮点:
This note mainly analyze the persistence of data is also:
ViewModel使用
添加依赖
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
定义ViewModel
public class MyViewModel extends ViewModel {
// There can be any variable
public int number;
}
使用ViewModel
public class MainActivity extends AppCompatActivity {
private MyViewModel myViewModel;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
mTextView = findViewById(R.id.text);
mTextView.setText(myViewModel.number + "");
}
public void onTextView(View view) {
myViewModel.number = myViewModel.number + 1;
mTextView.setText(myViewModel.number + "");
}
}
运行上面代码以后,When user change configuration such as the screen,ViewModelSave data is not lost;
ViewModel原理分析
至于ViewModelOnline there are a lot of the cause of the data is stored in the content,说白了就是一句话,The above data are stored in public memory;
涉及到两个方法:
onRetainNonConfigurationInstance() 是在Activity 销毁的时候 进行存储信息.
getLastNonConfigurationInstance() 的作用是获取onRetainNonConfigurationInstance存储的信息.
当屏幕发生旋转的时候 ,会先调用 onRetainNonConfigurationInstance先将数据进行保存,然后再通过 getLastNonConfigurationInstance 将保存的数据获取到.
The inside of the specific code is no longer a list,有兴趣可以自己查看;
copyright notice
author[Cloud valley area people],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2023/025/202301251120395567.html
The sidebar is recommended
- The front end of actual combat: copy write millet's official website on the first day
- 1. Linux application programming and network programming---file IO notes in Linux system
- [Front-end notes - CSS] 10. Cascading and inheritance + selector
- 8. Linux application programming and network programming---Linux network programming notes
- [Java|golang] 1828. Count the number of points in a circle
- [Front-end notes——CSS] 11. Box model + background and border
- C + + large Numbers together, according to a combined
- 3. Linux application programming and network programming --- get system information notes
- The use and principle of Kafka message queue
- 5. Linux application programming and network programming---signal notes in Linux
guess what you like
6 · Linux application programming and system programming - senior IO notes
Java collection common interview questions (4)
072-JAVA project training: Imitation QQ instant messaging software series lecture 7 (explaining the realization of the chat interface and functions)
Coordination center performance comparison: how zookeeper solves the load balancing problem
070-JAVA project training: imitation QQ instant messaging software series lecture five (explain user registration function)
Ubuntu installation and configuration (brief)
073-JAVA project training: imitation QQ instant messaging software series lecture eight (explain query and add friend function)
SQL injection classification and error injection EXP
All basic commands in linux fail, showing that the command cannot be found
4. Linux application programming and network programming---Linux process full solution notes (difference between process and program)
Random recommended
- Linux system - basic IO
- Hanlp's understanding of user-defined dictionaries (java version)
- Brief description and configuration of Maven
- 071-JAVA project training: imitation QQ instant messaging software series lecture six (explaining the function of QQ main interface)
- 【Maximum LeetCode】January Algorithm Training (12) Linked List
- 【Max LeetCode】January Algorithm Training (13) Doubly Linked List
- [Big Data Management] Java implements Bloom filter
- [Maximum LeetCode] Algorithm training in January (14) stack
- [Machine Learning] Adaboost Integrated Algorithm
- [Big Data Management] Java implements cuckoo filter (CF)
- Chaozhou Xiangqiao: "Charming Ancient City, Cultural Sharing" Spring Festival Intangible Cultural Heritage Market Opens
- [Big data management] Java realizes the dictionary tree TireTree
- [Max LeetCode] January Algorithm Training (11) Matrix
- New Express (Web framework based on HTTP module encapsulation NodeJS)
- JavaScript error-prone questions (stack processing, call function, prototype chain questions)
- Space "travel", lion and crane dance, intangible cultural heritage experience...During the Spring Festival, Zhuhai Jinwan is so fun!
- Wine 8.0 official release: better support for running Windows applications on Linux and other systems
- Zhongke Sugon: Sugon's new computer "participates in" "The Wandering Earth 2"
- Linux actual combat notes finishing (1.24)
- Winter Vacation JavaScript Tutorial Chapter 1 Variable Promotion
- Automatically execute the specified sql when the springBoot project starts
- Winter vacation javascript cram school Chapter 2 let & const temporary dead zone
- Microsoft CEO Nadella: We firmly believe that the wave of artificial intelligence is unstoppable
- [Bi She] Style Migration Based on CycleGAN [3] Code Migration to Server (Linux) and Environment Construction
- Axiba HTML
- Explore the vachar, test, longtext storage limits of the database mysql
- Web front-end development technology: Web front-end development technology
- Python data structure implements various sorting algorithms (including algorithm introduction and stability, complexity summary)
- Web front-end development technology: Vue routing
- 【 MySQL 】 the seventh part of the process control function
- Web front-end development technology: Vuex state management
- Important concepts and installation of Linux + vim common operations
- [MySQL] Part 9 MySQL Information Functions
- [MySQL] Part VIII Encryption and Decryption Functions
- PAT 1014 Sherlock Holmes' appointment (C++ implementation)
- Android gradle custom plug-in implementation
- Python numpy ValueError: setting an array element with a sequence.
- On Spring notes (annotation-based development)
- Ubuntu18.04 install Hadoop
- Android proguard code obfuscation and decompilation tool