How to make an activity window stay always on top
Asked Answered
L

4

9

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

Lastex answered 27/11, 2010 at 11:15 Comment(1)
You can do this with the help of WindowManager class.Margalit
A
5

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

Accost answered 27/11, 2010 at 11:36 Comment(5)
Ok, then how I would implement something similar to this ?appbrain.com/app/screen-filter/com.haxorLastex
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.Accost
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 : 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.Lastex
@RichardLeMesurier that's pretty interesting, I wonder how they did that and whether it works stable. Have you tried LilyPad Chat yet?Accost
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.Accost
C
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 :)

Cerenkov answered 28/5, 2013 at 11:58 Comment(4)
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!)Rojo
and it doesn´t work if user pressed something like contacts or system settings. this UIs will stay on top, any suggestions?Penman
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,Duvetyn
@Opiatefuchs: please post your code if it is yet relevant, you might do something wrong.Cerenkov
A
5

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

Accost answered 27/11, 2010 at 11:36 Comment(5)
Ok, then how I would implement something similar to this ?appbrain.com/app/screen-filter/com.haxorLastex
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.Accost
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 : 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.Lastex
@RichardLeMesurier that's pretty interesting, I wonder how they did that and whether it works stable. Have you tried LilyPad Chat yet?Accost
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.Accost
E
5

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:

Exactly answered 15/10, 2012 at 14:23 Comment(0)
L
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();
        }
    }
    
Limicolous answered 31/10, 2019 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.