简体   繁体   English

找到NFC标签后,活动将重新打开

[英]Activity reopens when a NFC Tag is found

I'm developing an app that should detect NFC tags. 我正在开发一个应检测NFC标签的应用程序。 My problem is that my activity reopens every time the app scans a tag. 我的问题是,每次应用程序扫描标签时,我的活动都会重新打开。 It should just open when the app is closed. 它应仅在应用程序关闭时打开。 But when it is active, I just want the data from the tag. 但是,当它处于活动状态时,我只想要来自标记的数据。

Manifest: 表现:

?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.corinna.nfc_testapp" >

<uses-permission android:name="android.permission.NFC" />
<uses-sdk android:minSdkVersion="14"/>
<uses-feature android:name="android.hardware.nfc"    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
</application>

</manifest>

Activity onCreate & handleIntent 活动onCreate&handleIntent

rotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    //Fragment ReadOrWrite
    fRead = (Fragment_Read) Fragment.instantiate(getApplicationContext(), Fragment_Read.class.getName(), null);
    Fragment_ReadOrWrite fReadOrWrite = (Fragment_ReadOrWrite) Fragment.instantiate(getApplicationContext(), Fragment_ReadOrWrite.class.getName(), null);
    fReadOrWrite.setFragment_Read(fRead);
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.id_activity_main, fReadOrWrite);
    fragmentTransaction.commit();


    //NFC

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    handleIntent(getIntent());
}

private void handleIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

        String type = intent.getType();
        if (MIME_TEXT_PLAIN.equals(type)) {

            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            NdefReaderTask ndefReader = new NdefReaderTask(fRead);
            ndefReader.execute(tag);

        } else {
            System.out.println("Wrong mime type: " + type);
        }
    }
}

Just use launchMode="singleTop" in your main activity manifest. 只需在您的主要活动清单中使用launchMode="singleTop" This will ensure that if your activity is at the top of your task stack, it will not be recreated. 这将确保如果您的活动位于任务堆栈的顶部,则不会重新创建该活动。

Be aware that onCreate is no longer called in this case, so if you want to read the content from intent, you need to override the onNewIntent Activity method. 请注意,在这种情况下将不再调用onCreate ,因此,如果您想从intent中读取内容,则需要覆盖onNewIntent Activity方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM