تشخیص حالت Background در اپلیکیشن های react native


در گوشی های android و ios اپلیکیشن ها در دو حالت اجرا می شوند.اولین حالت که به Foreground شناخته می شود، اپلیکیشن باز است و کاربر در حال کار با آن است،دومین حالت Background است که اپلیکیشن در background باز است اما کاربر در حال کار با یک اپلیکیشن های دیگر است.کامپوننت AppState در react native برای تشخیص اینکه اپلیکیشن در چه حالتی است استفاده می شود و یک string را بر می گرداند.بنابراین در این آموزش متنی قصد داریم یاد بگیریم که چجوری با استفاده از AppState تشخیص بدیم که اپلیکیشن های react native در کدام حالت Foreground یاBackground هستند.
1.ایمپورت کامپوننت های StyleSheet, Text, View و AppState در فایل App.js
1 2 3 |
import React, { Component } from 'react'; import { StyleSheet, Text, View, AppState } from 'react-native'; |
2.تعریف ()constructor در کلاس اصلی و تعریف کردن یک state به نام appState.ما قصد داریم که مقدار AppState.currentState را در این state نگه داری کنیم.
1 2 3 4 5 6 |
constructor() { super(); this.state = { appState: AppState.currentState, } } |
3.تعریف ()componentDidMount در کلاس اصلی و فراخوانی ()AppState.addEventListener در آن.ما تابع ()handleAppStateChange_ را فراخوانی خواهیم کرد.این method زمانی که اپلیکیشن استارت می شود یا در حالت foreground است فراخوانی می شود.
1 2 3 |
componentDidMount() { AppState.addEventListener('change', this._handleAppStateChange); } |
4.تعریف ()componentWillUnmount. در این method ما تابع ()AppState.removeEventListener را فراخوانی می کنیم.این method زمانی که اپلیکیشن به حالت background می رود فراخوانی می شود.
1 2 3 |
componentWillUnmount() { AppState.removeEventListener('change', this._handleAppStateChange); } |
5.تعریف تابع ()handleAppStateChange_ با پارامتر nextAppState .این تابع نقش اساسی و کلیدی در این آموزش دارد.در این تابع ما مقدار appState را چک می کنیم.
مقادیر تعریف شده برای App States:
1.active : اپلیکیشن در حالت foreground در حال اجرااست.
2.background: اپلیکیشن در حالت background یا minimize است.
3.inactive: اپلیکیشن در حالت inactive است.این حالت زمانی اتفاق می افتد که برنامه بین حالت های foreground و background در حال جابجایی است.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
_handleAppStateChange = (nextAppState) => { this.setState({ appState: nextAppState }); if (nextAppState === 'background') { // Do something here on app background. console.log("App is in Background Mode.") } if (nextAppState === 'active') { // Do something here on app active foreground mode. console.log("App is in Active Foreground Mode.") } if (nextAppState === 'inactive') { // Do something here on app inactive mode. console.log("App is in inactive Mode.") } }; |
6.تعریف کامپوننت Text دورن بلاک render’s return و نشان دادن مقدار appState درون آن.
1 2 3 4 5 6 7 8 9 |
render() { return ( <View style={styles.MainContainer}> <Text style={styles.text}>Current state is: {this.state.appState}</Text> </View> ); } |
7.ایحاد استایل سفارشی.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const styles = StyleSheet.create({ MainContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, text: { fontSize: 20, color: '#000' } }); |
8.کد کامل برنامه در فایل App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import React, { Component } from 'react'; import { StyleSheet, Text, View, AppState } from 'react-native'; export default class App extends Component<> { constructor() { super(); this.state = { appState: AppState.currentState, } } componentDidMount() { AppState.addEventListener('change', this._handleAppStateChange); } componentWillUnmount() { AppState.removeEventListener('change', this._handleAppStateChange); } _handleAppStateChange = (nextAppState) => { this.setState({ appState: nextAppState }); if (nextAppState === 'background') { // Do something here on app background. console.log("App is in Background Mode.") } if (nextAppState === 'active') { // Do something here on app active foreground mode. console.log("App is in Active Foreground Mode.") } if (nextAppState === 'inactive') { // Do something here on app inactive mode. console.log("App is in inactive Mode.") } }; render() { return ( <View style={styles.MainContainer}> <Text style={styles.text}>Current state is: {this.state.appState}</Text> </View> ); } } const styles = StyleSheet.create({ MainContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, text: { fontSize: 20, color: '#000' } }); |
اسکرین شات:
اسکرین شات از پنجره Command prompt بعد از اجرای دستور react–native log–android
دیدگاهتان را بنویسید