Unable to remove activity from stack,

I am starting an activity from service. ,

In this i set two flags one is

Intent.FLAG_ACTIVITY_NEW_TASK because i am running activity from service.its new

Intent.FLAG_ACTIVITY_NO_HISTORY is set to new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished.

Here is the code which i call from service to open activity

 public static void open(Context context){

        Intent intent=new Intent(context,PopupActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
        context.startActivity(intent);    
    }

and i have cross button in my activity to destroy activity , when i press cross button activity should remove from stack,but it remain in stack and when press home button to view it, activity recreated in stack.for example "oncreate" is again called

here is my code.

 @OnClick(R.id.flClose) void closeWindow(){

       finish();
    }

4 answers

  • answered 2019-03-14 04:22 Hemil Kumbhani

    Try using finishAffinity() instead of finish()

  • answered 2019-03-14 04:22 bhagyawant biradar

    Add the following flags instead of setflags to open activity

    intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    

    And on close button call the function below

    private void endTask() {
            if (Build.VERSION.SDK_INT >= 21) {
                finishAndRemoveTask();
            } else {
                finish();
            }
        }
    

  • answered 2019-03-14 05:39 Android enthu.

    You can get this behaviour by setting the android:noHistory attribute to "true" in the relevant entries in your AndroidManifest.xml file.

    For example:

    <activity
        android:name=".YourActivity"
        android:noHistory="true" />
    

  • answered 2019-03-14 08:18 Amkhan

    I solved My problem by Adding flag Intent.FLAG_ACTIVITY_NEW_DOCUMENT This flag is used to open a document into a new task rooted at the activity launched

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
    

    In manifest i have noHistory

    android:noHistory="true"