Home > front end >  onResume called for background activity when showing transparent activity
onResume called for background activity when showing transparent activity

Time:11-09

I have a requirement that asks to call an API from onResume of Activity-A and its response is used to block users from using the app if required by showing an Activity-B on top of Activity-A.

Another requirement was to have a few messages in the middle of Activity-B's UI and have the rest of the screen of Activity-B transparent. These messages show the user why they were blocked and redirect them to a URL to get themselves unblocked.

When the user returns from the redirected URL, the same API is called from Activity-B's onResume to check the current status.

Now the problem is, whenever the app is brought back from the background to the foreground when Activity-B is showing, onResume of Activity-A is also called which causes a double API call.

I've used the following theme style to make my Activity-B transparent:

<style name="Theme.AppCompat.Transparent.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

and then setting this theme in the manifest of Activity-B

CodePudding user response:

in Activity-B

public void onPause(){
    finish();
    super.onPause();
}

This will finish Activity-B and the next time the user goes back to the app Activity-A will handle the api call like normal and recreate Activity-B if necessary.

Although I don't know your specific use case but you could just have a GONE overlay that is the size of the screen. When the user is not supposed to use the app the overlay is then set to VISIBLE. All actions are consumed by the overlay instead of the views beneath it effectively locking the user out and you would avoid a whole extra activity.

  • Related