0

I have a particular case where I need to ask something to the user when he starts a printer service. So from the onStartPrinterDiscovery (so a service), I start an activity to display the dialog and when the action is done, I send a new intent which calls finish() nd so I see that onDestoy() is called. Unfortunately when I hit the apps history button, I still see my activity's screen behind: Activity'ss screen

Could you tell me why and how to fix it please?

androidManifest.xml:

    <activity
        android:name=".DialogActivity"
        android:noHistory="true"
        android:theme="@android:style/Theme.Dialog"
        android:launchMode="singleTop">
    </activity>

DialogActivity:

class DialogActivity : Activity() { var activity:Activity = this

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.floatingactivity)
        setFinishOnTouchOutside(false)

        onNewIntent(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        isDisplayed = false
    }

    override fun onNewIntent(intent: Intent) {
        val extras = intent.extras
        val action = extras.getString("action")

        when (action) {
            "showDialog" -> {
                if (!isDisplayed) {
                    tvMessage.text = getString(R.string.ask_for_action)
                    isDisplayed = true
                }
            }
            "showErrorDialog" -> {
                if (!isDisplayed) {
                    tvMessage.text = getString(R.string.error_action)
                    isDisplayed = true
                }
            }
            "dismissDialog" -> { activity.finish() }
            else -> {}
        }

        if (isDisplayed) {
            btCancel.setOnClickListener {
                activity.finish()
            }
        }
    }

    companion object {
        var isDisplayed = false
    }
}

EDIT I add how I currently create my Intent because of one answer which could be a solution:

    val intent = Intent(applicationContext, DialogActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
    intent.putExtra("action","showDialog")
    startActivity(intent)
fralbo
  • 2,534
  • 4
  • 41
  • 73
  • Please check https://stackoverflow.com/a/14523375/3858030 – Ufkoku Jul 12 '18 at 08:29
  • Possible duplicate of [Android - How to remove activity from recent apps?](https://stackoverflow.com/questions/14523287/android-how-to-remove-activity-from-recent-apps) – Vikasdeep Singh Jul 12 '18 at 08:31

2 Answers2

1

Add the android:excludeFromRecents to your activity attributes, like so:

<activity
    android:name=".DialogActivity"
    android:theme="@android:style/Theme.Dialog"
    android:excludeFromRecents="true"
    android:launchMode="singleTop">
</activity>

This is from the docs of the android:excludeFromRecents:

Whether or not the task initiated by this activity should be excluded from the list of recently used applications, the overview screen. That is, when this activity is the root activity of a new task, this attribute determines whether the task should not appear in the list of recent apps. Set "true" if the task should be excluded from the list; set "false" if it should be included. The default value is "false".


The android:noHistory="true" is for different purposes. This is from the docs of the android:noHistory:

A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it. In this case, onActivityResult() is never called if you start another activity for a result from this activity.

HedeH
  • 2,869
  • 16
  • 25
0

Try this:-

    Intent i = new Intent(this,YourFirstActivity.Class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(i);
    finish();

Or

use finishAffinity() suitable for >= API 16.

Mr. Roshan
  • 1,777
  • 13
  • 33