How to Detect When the App Goes to Minimized or Exit

How to detect when an Android app goes to the background and come back to the foreground

The onPause() and onResume() methods are called when the application is brought to the background and into the foreground again. However, they are also called when the application is started for the first time and before it is killed. You can read more in Activity.

There isn't any direct approach to get the application status while in the background or foreground, but even I have faced this issue and found the solution with onWindowFocusChanged and onStop.

For more details check here Android: Solution to detect when an Android app goes to the background and come back to the foreground without getRunningTasks or getRunningAppProcesses.

How could I detect when my application is minimized?

When the user minimizes the window (either using the box on the title bar, or by selecting the "Minimize" option from the system menu), your application will receive a WM_SYSCOMMAND message. The wParam parameter of that message will contain the value SC_MINIMIZE, which indicates the particular type of system command that is being requested. In this case, you don't care about the lParam.

So you need to set up a message map that listens for a WM_SYSCOMMAND message with the wParam set to SC_MINIMIZE. Upon receipt of such a message, you should execute your code to minimize your application to the taskbar notification area, and return 0 (indicating that you've processed the message).

I'm not sure what GUI framework you're using. The sample code could potentially look very different for different toolkits. Here's what you might use in a straight Win32 C application:

switch (message)
{
case WM_SYSCOMMAND:
if ((wParam & 0xFFF0) == SC_MINIMIZE)
{
// shrink the application to the notification area
// ...

return 0;
}
break;
}

How to detect when an Android application is minimized?

If orientation changes the app will call through the life cycle once again that means from oncreate

you can avoid it as well by writing the following to code to the manifest

 <activity
android:name=""
android:configChanges="orientation|keyboardHidden|screenLayout|screenSize"
android:label="@string/app_name" />

this tell the system that when orientation changes or keyboardHidden or screenLayout changes I will handle it by myself no need to re create it.

then write your code on on pause

How to have page re-rendered after exiting and re-opening the app?

If you don't mean closing the app and rather just sending it to the background, you can use the exported AppState object to detect it. See the answer to this question for an example. Once you detect the event, you can force a rerender using forceUpdate() for class components, or by using a dummy hook in functional components

Slightly modified React docs example:

import React, {Component} from 'react';
import {AppState, Text} from 'react-native';

class AppStateExample extends Component {
state = {
appState: AppState.currentState,
};

componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}

componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}

_handleAppStateChange = (nextAppState) => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === 'active'
) {
console.log('App has come to the foreground!');
this.forceUpdate();
}
this.setState({appState: nextAppState});
};

render() {
return <Text>Current state is: {this.state.appState}</Text>;
}
}

Functional component (untested):

import React, { useState, useEffect } from 'react';
import { AppState } from 'react-native';

const App = () => {
const [updateVal, setUpdateVal] = useState(false);
const forceUpdate = newState => {
if (newState === 'active')
setUpdateVal(!updateVal); // forces a rerender
}
useEffect(() => {
AppState.addEventListener('change', forceUpdate);
return () => AppState.removeEventListener('change', forceUpdate);
}, []);
// return your contents
};

However, if you're actually closing it (not just leaving the app), it should rerender.



Related Topics



Leave a reply



Submit