0

I've an Activity with a Notification area, when the app is minimized and I click on 'item' of notification area the App is maximized, but I want to leave it minimized.

There is a similar question Android notification Resume Activity but doesn't work for version 33 because I can't set only FLAG_CANCEL_CURRENT for item1PIntent

this is the code


class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        showNotification()
    }

    private fun showNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name: CharSequence = "My Notification"
            val description = "My notification description"

            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val notificationChannel = NotificationChannel("CHANNEL_ID", name, importance)
            notificationChannel.description = description
            val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)
            notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
        }

        val item1 = Intent(this, MainActivity::class.java)

        item1.putExtra("NotificationMessage", "extra1")
        item1.action = "A"
        item1.flags =   Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP

        val item1PIntent = PendingIntent.getActivity(
            this,
            0,
            item1,
            FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT
        )

        val builder = NotificationCompat.Builder(this, "CHANNEL_ID")

        builder.setSmallIcon(R.mipmap.ic_launcher)

        builder.addAction(
            com.google.android.material.R.drawable.ic_arrow_back_black_24,
            "Item 1",
            item1PIntent
        )

        builder.setContentIntent(item1PIntent)
        builder.setOngoing(true);

        builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))

        val notificationManagerCompat = NotificationManagerCompat.from(this)
        var notification = builder.build()

        notificationManagerCompat.notify(1, notification)
    }



gekomad
  • 525
  • 9
  • 17
  • Please clarify "I've an Activity with a Notification area, when the app is minimized and I click on 'item' of notification area the App is maximized, but I want to leave it minimized." - What is "minimized", "item", and "maximized"? – dominicoder May 16 '23 at 17:23
  • @dominicoder Pressing the Middle (or Home) Button does "minimize" the App. The Item is the item in notification area – gekomad May 17 '23 at 12:00
  • 1
    OK - generally "minimize" and "maximize" are desktop concepts. On Android, apps are "sent to the background" or "brought to the foreground". And an "item" in the notification area is just "a notification". FYI in case you have more questions around this, it will be easier to communicate the issue using standard terms. – dominicoder May 17 '23 at 14:27

1 Answers1

0

I've an Activity with a Notification area, when the app is minimized and I click on 'item' of notification area the App is maximized, but I want to leave it minimized.

You are setting your PendingIntent on both the action and the "item" itself. If you don't want to launch the activity on clicking the item itself, don't set a content intent.

builder.addAction(
    com.google.android.material.R.drawable.ic_arrow_back_black_24,
    "Item 1",
    item1PIntent
)

builder.setContentIntent(item1PIntent) // <-- REMOVE THIS
dominicoder
  • 9,338
  • 1
  • 26
  • 32