Pending intent always make new activity

后端 未结 6 2107
耶瑟儿~
耶瑟儿~ 2020-12-20 14:51

i trying to make app with nfc function. the problem is when nfc tag discovered, pending intent always make a new activity that already exist. i\'m using tab host. how to mak

相关标签:
6条回答
  • 2020-12-20 14:57

    you can use the flag "PendingIntent.FLAG_UPDATE_CURRENT" while calling the "getActivity" method. I2m using it without a problem.

    0 讨论(0)
  • 2020-12-20 14:59

    I had a similar problem but not using a TabHost - every time an NFC tag was scanned my app was launching a new activity rather than firing onNewIntent as I wanted. I was trying to set android:launchMode="singleTask" in the manifest and use NfcAdapter.enableForegroundDispatch() in my activities onResume method. Instead, I abandoned using PendingIntent and set up intent filters my activity in the manifest as follows:

        <activity
            ...
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <data android:mimeType="application/vnd.myname.myapp" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
    
    0 讨论(0)
  • 2020-12-20 15:05

    use this flag instead..

    new Intent(this,
        getClass()).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
    
    0 讨论(0)
  • 2020-12-20 15:14

    Create your pending intent as below:

    PendingIntent.getActivity(this, 0,new Intent(this,
        getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
        |Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
        |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0);
    
    0 讨论(0)
  • 2020-12-20 15:19

    Put android:launchMode="singleTask" for your activity (or activities) in the manifest. That does the trick. Whenever an NFC intent is dispatched by the system, always a new Activity will be created. This is unique for NFC intents. So setting android:launchMode="singleTop" will not work, nor will setting flags in the PendingIntent.

    Another solution is to use NfcAdapter.enableForegroundDispatch() in all your Activities. That way your app gets to handle all NFC intents itself directly (via onNewIntent()).

    0 讨论(0)
  • 2020-12-20 15:20

    Try this:

    mNotificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    

    And in your manifest:

    android:launchMode="singleTask"
    
    0 讨论(0)
提交回复
热议问题