0

I am searching from 3 last Days that

How to remove activity from recent list (also known as overview screen, recent app list..etc)

Here is the my android manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abc.visibility">
 <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:noHistory="true"
            android:taskAffinity=""
            android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Here is the my JavaCode for MainActivity

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    protected void onResume() {super.onResume();}
    public void onWindowFocusChanged(boolean hasFocus) {if (!hasFocus) {clearTask();}}
    void clearTask(){if(android.os.Build.VERSION.SDK_INT >= 21){finishAndRemoveTask();} else{finish();}}
    @Override
    public void onBackPressed() {super.onBackPressed();}

}

So, my question is the simple.....

How to remove my activity from recent list

In my Case : Only one activity

  1. Activity needs to started.
  2. Once user click on Recent button (Android standard button)
  3. it need to finish and needs not show in recent list

I need this much only.

Help me friends if you really know how to do it....

dbg
  • 19
  • 6

3 Answers3

0

If I right understood and you want to remove your app from list of recent used apps when you press home button or whatever button then you can check this answer.

Community
  • 1
  • 1
thealeksandr
  • 1,646
  • 10
  • 17
  • thanks for respose, no, not like that when i press on onWindowFocusChange button that activity is removed from the task but still available in overview screen.. – dbg Jun 23 '16 at 09:38
0

In your Manifest.xml use this android:autoRemoveFromRecents="true".

      <activity
            android:name=".MainActivity"
            android:autoRemoveFromRecents="true">

In activity Change your code to this format.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      if(android.os.Build.VERSION.SDK_INT >= 21)
       {
        finishAndRemoveTask();
       }         
      else
        {
         finish();
        }
     }


 public static void exitApplication(Context context)
    {
        Intent intent = new Intent(context, MainActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

        context.startActivity(intent);

    }

}

and when os version is Api Level < 21 then use this code to close it.

MainActivity.exitApplication(context);

Hope this will help you.

Harshad Pansuriya
  • 17,218
  • 7
  • 58
  • 86
  • this is not the solution which i want.... My app have a single activity and it need to finish when i click on recent button in android standard buttons when i used this solution it will start another activity... so, this is not a solution which i am going to find... – dbg Jun 23 '16 at 10:03
  • @dhiren what you have write in the question read it again. you want remove activity from recent list. – Harshad Pansuriya Jun 23 '16 at 10:07
  • Really i want that only but using your solution : it will start Again MainActivity it self – dbg Jun 23 '16 at 10:10
  • @Dhiren wait my device create problem So i update it. – Harshad Pansuriya Jun 23 '16 at 10:32
  • @Dhiren I have check it see use only this code which i write in answer it will work. – Harshad Pansuriya Jun 23 '16 at 10:43
  • @Dhiren can you try it or not ?? – Harshad Pansuriya Jun 23 '16 at 10:45
  • -@Ironman it is working in that manner, i know it! but when onWindowFocusChanged() at that time that activity is deleted/removed from stack and than it is continue showing in recent list (that i don't want to show in stack at particular time)..... i am trying to find this solution from last 2 days and i am not able to find it... – dbg Jun 23 '16 at 10:50
  • so, any better suggestion for it? – dbg Jun 23 '16 at 10:52
  • @Dhiren it not display check also when `onWindowFocusChanged()`. – Harshad Pansuriya Jun 23 '16 at 10:53
  • in that manner also working but application behavior is not like that application continues running when user click on recent button it needs not show in recent list..... and using your code application not even start... – dbg Jun 23 '16 at 11:03
  • @Dhiren if application even not start then put this code on any `Onclcik` because the speed of Emulator you can not catch it can run 1000 of file in sec so that the code is runnning to fast..I can give my best 100% to you. – Harshad Pansuriya Jun 23 '16 at 11:07
  • simple 1. check out the question and in that code of MainActivity.java 2.Application Once Launched and 3. When User clicked onWindowFocusChanged() method once lost the focus 4. at particular time i need to finish the activity and remove that activity from recent list / overview screen – dbg Jun 23 '16 at 11:15
0

Using the AppTask class to remove tasks

 public void onRemoveFromRecents(View view) {

finishAndRemoveTask();

}

Retaining finished tasks

If you want to retain a task in the overview screen, even if its activity has finished, pass the FLAG_ACTIVITY_RETAIN_IN_RECENTS flag in the addFlags() method of the Intent that launches the activity.

 private Intent newDocumentIntent() {

       final Intent newDocumentIntent = new Intent(this,        NewDocumentActivity.class);

newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT 

  android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);

newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, 

incrementAndGet());

return newDocumentIntent;

}

here is the link from android docs this is you are demanding] here

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        TypedValue typedValue = new TypedValue();
        Resources.Theme theme = getTheme();
        theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
        int primaryColor = typedValue.data;

        Bitmap iconBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_activity_welcome);
        ActivityManager.TaskDescription td = new ActivityManager.TaskDescription(null, iconBitmap, primaryColor);

        setTaskDescription(td);
        iconBitmap.recycle();
    }
  • Thanks, But it will use for multitask and i already used **finishAndRemoveTask();** in my code. give me better suggestion... – dbg Jun 23 '16 at 11:59
  • thanks agian, I don't want to retain activity in overview screen. i really needs to remove it from overview scree when user click on recent menu button in android... – dbg Jun 24 '16 at 10:35