2

In my app i want to disable the recent apps button.

(i.e)When user press that button it doesn't show the recent running apps and reside on the same activity page.

I did that by following code. But it doesn't work in Android L.

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (!hasFocus) {
            windowCloseHandler.postDelayed(windowCloserRunnable, 250);

        }
    }

    private void toggleRecents() {
        Intent closeRecents = new Intent("com.android.systemui.recent.action.TOGGLE_RECENTS");
        closeRecents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        ComponentName recents = new ComponentName("com.android.systemui", "com.android.systemui.recent.RecentsActivity");
        closeRecents.setComponent(recents);
        this.startActivity(closeRecents);
        Toast.makeText(getApplicationContext(), "Complete the test and go back", Toast.LENGTH_LONG).show();
    }

    private Handler windowCloseHandler = new Handler();
    private Runnable windowCloserRunnable = new Runnable() {
        @Override
        public void run() {
ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            ComponentName cn = am.getRunningTasks(1).get(0).topActivity;



            if (cn != null && cn.getClassName().equals("com.android.systemui.recent.RecentsActivity")) {
                toggleRecents();
            }
        }
    };

Please help to solve this issue in Android Lollipop.

Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
Asha
  • 750
  • 1
  • 6
  • 22
  • Hie.AFAIK you cant disable the recent app button. but I have a solution with that your Activity will not seen in recent Apps. use this : android:excludeFromRecents="true" in all the Activities in your Menifest file. May be it will help you. – MDroid Jul 08 '15 at 11:59
  • or as per your requirement : http://stackoverflow.com/questions/14574239/how-to-disable-the-recent-tasks-apps-button-in-android – MDroid Jul 08 '15 at 12:05
  • @MDroid thanks for your response. But if i do like that your suggestion it didn't show my app in recent apps. Even though user presses the recent apps button my app control don't exit and shows the current activity page. – Asha Jul 08 '15 at 12:05
  • I get your issue. When user is in App and press recent app button--> and again start the app it will show the current Activity . Am i right ? – MDroid Jul 08 '15 at 12:09
  • @MDroid I already tried this( stackoverflow.com/questions/14574239/ ) link. From that only i got the code in my question. – Asha Jul 08 '15 at 12:10
  • Have you add this permission in menifest ? – MDroid Jul 08 '15 at 12:11
  • yes u are right. it will show the window which is currently accessed by user. At the same time the activity doesn't reload that is main concept. – Asha Jul 08 '15 at 12:14
  • AFAIK you cant override the Recent app button as we generally do with back button in Android! – MDroid Jul 08 '15 at 12:14
  • I added that permission in manifest file – Asha Jul 08 '15 at 12:15

1 Answers1

0

you can use the code below. but the problem is though, after someone click on recent app button, the action call after a second and if he/she quickly clear the app from the list, your app is lost.

 @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (!hasFocus) {
        // Close every kind of system dialog
        Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        sendBroadcast(closeDialog);
    }
}

Cheers!

jsina
  • 4,433
  • 1
  • 30
  • 28