Q:

Chathead to stay foreground when open a new activity

Normal function of chatHead is that it appears on all the activities whichever is opened. I would like the chatHead to stay in foreground when i open a new activity. For example, when i open another application like contacts or Message chatHead should not appear. Is this possible? Please advise with code thanks
Fly class:
public class Fly extends Service {

private WindowManager windowManager;
private ImageView chatHead;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override 
public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);

    chatHead.setImageResource(R.drawable.floating);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,

            WindowManager.LayoutParams.TYPE_PHONE,

            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    windowManager.addView(chatHead, params);

    try {
        chatHead.setOnTouchListener(new View.OnTouchListener() {
            private WindowManager.LayoutParams paramsF = params;
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    // Get current time in nano seconds.

                    initialX = paramsF.x;
                    initialY = paramsF.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                    paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                    windowManager.updateViewLayout(chatHead, paramsF);
                    break;
                }
                return false;
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
    }

}

@Override
public void onDestroy() {
    super.onDestroy();
    if (chatHead != null) windowManager.removeView(chatHead);
    //windowManager.removeView(chatHead);

}

}

MainActivity:
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Bundle bundle = getIntent().getExtras();

    if(bundle != null && bundle.getString("LAUNCH").equals("YES")) {
        startService(new Intent(MainActivity.this, Fly.class));
    }

    Button launch = (Button)findViewById(R.id.button1);
    launch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(MainActivity.this, Fly.class));
        }
    });

    Button stop = (Button)findViewById(R.id.button2);
    stop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(MainActivity.this, Fly.class));
        }
    });

}

@Override
protected void onResume() {
    Bundle bundle = getIntent().getExtras();

    if(bundle != null && bundle.getString("LAUNCH").equals("YES")) {
        startService(new Intent(MainActivity.this, Fly.class));
    }
    super.onResume();
}

protected void onPause() {
    if (chatHead != null) windowManager.removeView(chatHead);

    super.onPause();

}

}

A:

Your chathead is added to the WindowManager, which appears above everything else in your application, including newly launched activities. You should manually remove the chatHead in the onPause function of an Activity by messaging the Service if you do not want it to appear outside of that certain Activity.
This code will need to run in the Service when it receives the message from the activity.
if (chatHead != null) {
    windowManager.removeView(chatHead);
}

You can also save the parameters of the chatHead view and re-add it back by sending a different message to the Service in the Activity's onResume.
Please see the Android Service documentation for how to set up messaging.

Another alternative is to not use a service at all, but to move all of the code from it into your activity. Then, you can directly control whether the chatHead is displayed from the Activity's onPause and onResume functions. Please see this helpful tutorial on Pausing and Resuming an Activity.

