8

I want to create an activity that stays always on top (like the modal windows or task manager in Windows) of the others activities. How can I do this on Android? Thanks

Alex
  • 2,153
  • 4
  • 29
  • 43

4 Answers4

12

You can use the following code in the overridden onStop method of your activity:

@Override
protected void onStop(){
    super.onStop();
    Intent intent = new Intent(this, ClassNameOfYourActivity.class);
    startActivity(intent);
}

Beauty problem: your activity will pop-up again if any other activity trying to claim the focus. So it's not a modal window.

And it's dangerous! You wont be able to handle the Android GUI, you'll be able to control only your application GUI. For instance, switching debug mode on-off, killing application (only over ADB), reaching system settings, etc. will be impossible. If you switch off the ADB and combine it with the auto start mechanism then you'll be in trap.

So you won't be popular if you share it with Play :)

bdevay
  • 153
  • 1
  • 10
  • 1
    I used this workaround, with a flag, and a button! For error reporting within my app! Works like charm! (you have to use it wisely!) – Paschalis Jul 08 '13 at 17:09
  • and it doesn´t work if user pressed something like contacts or system settings. this UIs will stay on top, any suggestions? – Opiatefuchs Feb 26 '15 at 19:51
  • 1
    Oh I agree that it is dangerous, but it's perfect for my purposes. I develop mainly for my kid's kindles and an app that needs mom or dad to authorize additional time is just want I needed. They may hate it, but they can't stop me. Thanks, – BSD Sep 25 '16 at 21:43
  • @Opiatefuchs: please post your code if it is yet relevant, you might do something wrong. – bdevay Sep 26 '16 at 19:42
4

Depending on what exactly you are trying to do, you might be happy with windows that stay on top of other Activities.

Apps like Super Video client & Floating Notes are able to display floating windows above other activities. These questions might point you in the right direction:

Community
  • 1
  • 1
Richard Le Mesurier
  • 27,993
  • 19
  • 127
  • 242
4

You can't. As this is defined in the Android system.

Sebastian Roth
  • 10,454
  • 13
  • 56
  • 103
  • Ok, then how I would implement something similar to this ?http://www.appbrain.com/app/screen-filter/com.haxor – Alex Nov 27 '10 at 11:38
  • See - whenever you open a activity it's in the foreground and that's about as much as u can get. You can overwrite a number of intents (LAUNCHER) to prevent people from escaping your activity, but this won't work safely in the long term. You can compile a custom build of Android for your phone. Or, do it the Android way and make a sticky Notification (see "Advanced Task Killer" for reference) - then you have a icon always staying in the notification area. – Sebastian Roth Nov 27 '10 at 11:45
  • Thanks, I don't really need to have an activity stays always on top, just this was my thinking was about how to implement that screen filter from here : http://www.appbrain.com/app/screen-filter/com.haxor .So it might just not be the right way of doing this, I just need a starting point about how this can be achieved. – Alex Nov 27 '10 at 12:04
  • @RichardLeMesurier that's pretty interesting, I wonder how they did that and whether it works stable. Have you tried LilyPad Chat yet? – Sebastian Roth Oct 12 '12 at 17:10
  • One question: Once this floats on top of the movie player for example - what happens if you touch the movie player? Will it continue to show the LilyPad chat window? I'm checking the answers you referred to, very interesting, however it sounds quite "hackish" to use a system alert for regular apps. My answer is incorrect now though. Will check and edit later. – Sebastian Roth Oct 15 '12 at 17:10
0

Follow the steps to achieve your requirement

  1. Create an activity which is going to be the top activity
  2. Add the following code in the manifest

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    
  3. Use the following code to get overlay permission from user

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            new AlertDialog.Builder(this)
                .setTitle("Permission Request")
                .setMessage("This app needs your permission to overlay the system apps")
                .setPositiveButton("Open settings", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                        startActivityForResult(myIntent, 101);
                    }
                })
                .setNegativeButton(android.R.string.no, null)
                .show();
        }
    }
    
Community
  • 1
  • 1
dilanMD
  • 85
  • 11