Friday 12 March 2021

How can I know an Activity launched from dynamic shortcuts when I use singleTask?

ActivityMain is singleTask.

I can use Code A to create a dynamic shortcuts icon in screen, it will show ActivityMain when I click the shortcuts icon.

You know I can also show ActivityMain by clicking the app icon.

How can I know a Activity lanuched from dynamic shortcuts when I use singleTask ?

BTW, Code B doesn't work.

Code A

 @TargetApi(25)
    private fun createShorcut() {
        val sM = getSystemService(ShortcutManager::class.java)
        val intent1 = Intent(this, ActivityMain::class.java)

        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent1.setAction("My")

        val shortcut1 = ShortcutInfo.Builder(this, "shortcut1")
            .setIntent(intent1)
            .setShortLabel("Shortcut 1")
            .setLongLabel("Shortcut 2")
            .setShortLabel("This is the shortcut 1")
            .setDisabledMessage("Login to open this")
            .setIcon(Icon.createWithResource(this, R.drawable.notify_icon))
            .build()
        sM.dynamicShortcuts = Arrays.asList(shortcut1)
    }


    @TargetApi(25)
    private fun removeShorcuts() {
        val shortcutManager = getSystemService(ShortcutManager::class.java)
        shortcutManager.disableShortcuts(Arrays.asList("shortcut1"))
        shortcutManager.removeAllDynamicShortcuts()
    }


    if (Build.VERSION.SDK_INT >= 25) {
            createShorcut()
    }else{
            removeShorcuts()
    }

   <activity android:name=".ui.ActivityMain"
           android:launchMode="singleTask">
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
   </activity>

Code B

  override fun onResume() {
        super.onResume()

        if (mActivity.intent.action.equals("My")) {
            toast("ActivityMain from dynamic shortcuts")
        }
   }

Added Content:

To prateek: Thanks!

Do you mean I have to check it both onCreate and onNewIntent event when I use SingleTask?

In SingleTask:

A: When I run the APP (Click either App Icon or Shortcut Icon) for the first time, only onCreate event is fired.

B: If the app is already running, when I fire the App again (Click either App Icon or Shortcut Icon) , only onNewIntent event is fired.

So I have to check it both onCreate and onNewIntent event just like Code C, right?

Code C

  class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.btnClose).setOnClickListener {
            finish()
        }

        createShorcut()

        var which= this.intent.getBooleanExtra("yourkey", false)

        displayResult(which)
    }


    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)

        var which= intent?.getBooleanExtra("yourkey", false)

         displayResult(which?:false)
    }

    private fun displayResult(which:Boolean){
        if (which) {
            Toast.makeText(this, "From ShortCut Icon", Toast.LENGTH_LONG).show()
        }else{
            Toast.makeText(this, "From App Icon", Toast.LENGTH_LONG).show()
        }
    }

    private fun createShorcut() {
        val sM = getSystemService(ShortcutManager::class.java)
        val intent1 = Intent(this, MainActivity::class.java)

        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent1.setAction("My")
        intent1.putExtra("yourkey", true)

        val shortcut1 = ShortcutInfo.Builder(this, "ShortCut Item")
                .setIntent(intent1)
                .setShortLabel("Shortcut Item")
                .setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_foreground))
                .build()
        sM.dynamicShortcuts = Arrays.asList(shortcut1)
    }
}


      <activity
            android:name=".MainActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


from How can I know an Activity launched from dynamic shortcuts when I use singleTask?

No comments:

Post a Comment