Search code examples
androidandroid-activitybroadcastreceiver

Bring A Running Android App to Foreground


I am designing a testing tool that requires launching another app and then bring back my activity that I was working on to the foreground. I have been trying to tackle this problem using a receiver but no luck so far. Any suggestion would be very appreciated.


Solution

  • If you don't want to make anything special, than just add a post job (with some delay) to your current handler and send an Intent to start your Activity with flag bring to foreground.

    Example for clarification:

        /*
        * Your code to launch the 3rd party app
         */
        Handler handler=new Handler();
        handler.postDelayed(new Runnable(){
            public void run() {
                Intent openMe = new Intent(getApplicationContext(), MyActivity.class);
                openMe.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); //experiment with the flags
                startActivity(openMe);
            }}, 3000);
    

    As the docu says, maybe you should use FLAG_ACTIVITY_REORDER_TO_FRONT

    You can also store information's inside of the Intent to retrieve later and rebuildt your screen.

    Example:

    intent.putExtra("someStringAsIdentifier", someInformationObject);
    

    Inside of you new activity in onCreate you do:

    Intent i = getIntent();
    Bundle extras = i.getExtras();
    if(extras != null){
        informationObject = extras.getExtra("someStringAsIdentifier");
    }
    

    See the docs of Intent for more information. :)