1

I have a service that handles the incoming in-app message. The app has a SplashActivity, MainActivity and a second Activity which is called to display this in-app message from the Service. The in-app is getting displayed in the SplashActivity if the mainActivity takes a longer time to launch. How do I ensure that the in-app message activity(which is initiated by the service) is called only after the MainActivity is being displayed to the user.

I saw a similar issue in Firebase In-App Messaging showing in SplashActivity. How to show it in MainActivity?, but I have my own implementation of the in-app message handling.

Tulika
  • 625
  • 1
  • 8
  • 23

2 Answers2

0

How do I ensure that the in-app message activity(which is initiated by the service) is called only after the MainActivity is being displayed to the user.

You should wait for your activity views to lay out and then display your message, to do that you can add a global layout listener to view tree observer of one of your activity views:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view = findViewById(R.id.some_view_id);

        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                // Display your in-app message here
            }
        }
    }
}
Sdghasemi
  • 5,370
  • 1
  • 34
  • 42
  • But from the sample code you specified, this would be triggered only once the MainActivity is called. But in my case since there is a service which is responsible to display the In-App message activity that would already have started running in parallel. – Tulika Jul 08 '20 at 04:45
0

I think your problem can be avoided by not devoting an activity to your splashscreen but rather doing your splashscreen the "right way" here is a link on the article on this. this implies simply using your splash screen in the background of your main activity while it loads.

ASLAN
  • 629
  • 1
  • 7
  • 20
quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
  • As per the link shared, I do need a dedicated SplashScreen Activity since I need to route to different screens based on some logic. So in my case, I use the Method 2 of the link for a SplashScreen scenario. – Tulika Jul 13 '20 at 09:39