Disable recent tasks button on Android 5.0
Asked Answered
G

2

2

I am working on an application that needs to suppress the recent apps button as it is done in the Toddler Lock application. What I want is user should not be able to exit my application by pressing the recent apps button.

In toddler lock (https://play.google.com/store/apps/details?id=marcone.toddlerlock&hl=en) whenever you press the recent apps button the screen flashes a bit but the app return to its own activity. Even if we press the recent apps button multiple times in quick succession and manage to display the recent apps screen after a few milliseconds the app return to its own activity.

I know there must be some service that which is behind the scenes but i cannot exactly figure out.

Can anyone tell me how does the Toddler Lock application manages to suppress the recent apps button. I need to implement exactly same behavior in my application.

I tried the answer that is given on the link below: Altering the result of getRecentTasks

and android intercept recent apps button but these does not work on Android Lollipop.

If there is a way to do this using a home screen in my application please consider that as well.

Gagliano answered 28/5, 2015 at 4:54 Comment(0)
G
11

Finally, I was able to achieve this by following:

@Override
protected void onPause() {
super.onPause();
    ActivityManager activityManager = (ActivityManager)getApplicationContext()
        .getSystemService(Context.ACTIVITY_SERVICE);
    activityManager.moveTaskToFront(getTaskId(), 0);
}

Will need to add this permission...

<uses-permission android:name="android.permission.REORDER_TASKS" />
Gagliano answered 5/6, 2015 at 11:32 Comment(4)
Hi Dpk this works for recent button fine. I want my home button as like Toddler Lock app. can you please help me?Dicentra
Have a look at these #12048635 and #6319560Gagliano
tested working with android 4.4.4, samsung galaxy mega. thanks!Hypogene
It is not working on to the Android 8.1 tablet 7 WSVGA(Tablet)....... , It is worked in Samsung Galaxy c7PRO.. Any solution for tablet?Wakeup
R
1

You have to write a service which will continuously monitor the top activity. If the top activity is from the package com.android.systemui means the user pressed the recent apps button. So at this time you have maintain the top activity from your application and start the same activity again.

Rhombencephalon answered 28/5, 2015 at 6:26 Comment(1)
ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfo =activityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo runningTaskInfo = taskInfo.get(0); ComponentName topActivity = runningTaskInfo.topActivity; I guess you are talking about this code and then check if the top activity is from com.android.systemui but i could not make this work on Lollipop although it works on Kitkat. On Lollipop it always reurns the activity from my app.Gagliano

© 2022 - 2024 — McMap. All rights reserved.