0

The problem seems to be known, but I could not find the right solution.


I will describe the scenario:


There is an application making requests to the API. In some FirstActivity, a request is made to the API, upon positive result of which startActivity () is called in SecondActivity. The problem is that if, while sending the request, the application is minimized to the background (that is, startActivity () will be called in the background), then:

  1. If android version> = 29 then startActivity () basically won't work. The one following startActivity () finish () will work and upon restarting the application will restart (which is logical)
  2. If the android version is < 29, then startActivity () will fire and bring this SecondActivity to the foreground.

Based on this, the question is. How can I force the application, regardless of version, to transition between activities and not bring them to the front?

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
            finish();
AlmecDoux
  • 3
  • 2
  • 1
    In terms of the transition, perhaps these should not be separate activities, but rather a single activity with multiple fragments or composables for the screens. The user will bring your app back to the foreground when the user decides to do so. – CommonsWare Dec 23 '21 at 14:20
  • @CommonsWare,These are the conditions of the problem. I need to use activities. – AlmecDoux Dec 23 '21 at 14:34

1 Answers1

0

As per documentation

Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background.

Workaround : In specific situations, your app might need to get the user's attention urgently, such as an ongoing alarm or incoming call. You might have previously configured your app for this purpose by launching an activity while your app was in the background.

To provide similar behavior on a device running Android 10 (API level 29) or higher, complete the steps described in this guide.

you can show a high-priority notification with a full-screen intent.

More Details

Updated answer for new requirement: For your comment (Well, please tell me how to make startActivity () in the background start the activity also in the background, and not raise the application from the background)

you can add a LifecycleObserver that will be notified when the LifecycleOwner changes state.

Inside your activity api response callback use the following condition

if (lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
            // Activity is in resumed state, Open new activity immediately
        } else {
            // else add a LifecycleObserver that will be notified when the LifecycleOwner changes state
            lifecycle.addObserver(object : DefaultLifecycleObserver {
                override fun onStart(owner: LifecycleOwner) {
                    super.onStart(owner)
                    // remove observer immediately so that it will not get triggered all the time
                    lifecycle.removeObserver(this)
                    // Activity is in start state again, Open new activity here
                }
            })
        }
Nitesh
  • 3,868
  • 1
  • 20
  • 26
  • This is not what I want. The user needs to click on the notifications. I need to secretly open the next Activity without distracting the user. Try to create a small project with two activities yourself and test startActivity in the background. – AlmecDoux Dec 24 '21 at 07:53
  • I understood your requirement... but that is not possible anymore... thats why i have mentioned the solution as a workaround not the exact solution. – Nitesh Dec 27 '21 at 16:55
  • Well, please tell me how to make startActivity () in the background start the activity also in the background, and not raise the application from the background – AlmecDoux Dec 28 '21 at 11:28
  • @AlmecDoux I have updated my answer, hope it will help you – Nitesh Dec 28 '21 at 14:03