-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
79 lines (69 loc) · 2.04 KB
/
App.tsx
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
69
70
71
72
73
74
75
76
77
78
79
import React, {useState, useEffect, useCallback} from 'react';
import {
AppState,
Button,
Linking,
SafeAreaView,
ScrollView,
StatusBar,
Text,
} from 'react-native';
import {AppLifecycle, useAppLifecycle} from 'react-native-applifecycle';
function App(): React.JSX.Element {
const currentLifecycle = useAppLifecycle();
const [lifecycleHistory, setLifecycleHistory] = useState<string[]>([
currentLifecycle,
]);
const [appStateHistory, setAppStateHistory] = useState<string[]>([
AppState.currentState,
]);
useEffect(() => {
const listener = AppLifecycle.addEventListener('change', state => {
setLifecycleHistory(prevState => [...prevState, state]);
});
return () => listener.remove();
}, []);
useEffect(() => {
const listener = AppState.addEventListener('change', state => {
setAppStateHistory(prevState => [...prevState, state]);
});
return () => listener.remove();
}, []);
const handleOpenSecondActivity = useCallback(async () => {
try {
/**
* Opens a second Android activity to demonstrate the difference between AppState and Lifecycle events.
*/
await Linking.openURL('sampleapp://second');
} catch (err) {
console.warn(err);
}
}, []);
return (
<SafeAreaView style={{padding: 24}}>
<StatusBar translucent={false} />
<ScrollView>
<Button
title="Open second activity"
onPress={handleOpenSecondActivity}
/>
<Text>Current Lifecycle: {currentLifecycle}</Text>
<Text style={{marginBottom: 12, marginTop: 24}}>
Lifecycle History:
</Text>
{lifecycleHistory.map((state, index) => (
<Text key={index}>
{index} {'->'} {state}
</Text>
))}
<Text style={{marginBottom: 12, marginTop: 24}}>AppState History:</Text>
{appStateHistory.map((state, index) => (
<Text key={index}>
{index} {'->'} {state}
</Text>
))}
</ScrollView>
</SafeAreaView>
);
}
export default App;