Search code examples
androiddialogandroid-launcher

Show dialog on a currently running app from my launcher app


So before people run and say thats its a duplicate let me explain.

I have an app which is basically a launcher in a tablet for a car. When ever I get a message from the server I show it as a pop up dialog with the below code -

public void openMessage(MsgItem msg)
{
    Intent intent = new Intent(_context, MainActivity.class);
    intent.putExtra(MainActivity.OP_EXTRA_MESSAGE, msg);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    _context.startActivity(intent);
}

Now you can see it returns to the MainActivity and then shows the pop up there.

I want the app to be able to show the pop up on what ever app is running inside it. That means if the user opened Waze app from the launcher and there is a pop up, it will show on the Waze activity and won't take me to the MainActivity first.

How can I fetch what app/activity is opened for this purpose? There are several apps that could be opened from the launcher beside the waze.

I saw other answers regarding the instances of the activities and such, but I didn't manage to implement anything in my code.


Solution

  • So after a lot of trying and failing, the solution was using a custom service with UI. A service which I add a view on it and call it as necessary.

    @CommonsWare's answer which I first chose as the right one was not what I needed because I wanted the pop up to show even if I'm in the Waze app for example and the solution using an activity with dialog theme couldn't do that because it has the app's context and so will always go back to the app before showing it.

    The only way to get this solution was by using a service with a dialog view.