【问题标题】:Dont start new activity on new intent, take the old activity不要以新的意图开始新的活动,采取旧的活动
【发布时间】:2014-11-05 08:11:55
【问题描述】:

我正在使用 NFC 并且正在处理意图。发现 NFC 标签时 我开始我的 NFCActivity。

<activity
    android:name="controller.NFCActivity" 
    android:launchMode="singleTask" >
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />
</activity>

NFCActivity 是一个没有 UI 的 Activity。它启动了我的 NFCReaderTask。

String action = intent.getAction();

if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    NFCDataDevice dataDevice = new NFCDataDevice();
    dataDevice.setCurrentTag(tagFromIntent);

    byte[] systemInfoAnswer = NFCCommand.sendGetSystemInfoCommandCustom(tagFromIntent, new NFCDataDevice());

    if (NDEFHelper.decodeSystemInfoResponse(systemInfoAnswer, new NFCDataDevice())) {
        NFCReaderTask nfcReaderTask = new NFCReaderTask(this, this, dataDevice);
        nfcReaderTask.execute();
    } 
}

当 ReaderTask 完成时,我的回调方法 onNFCReaderTaskCompleted 在我的 NFCActivity 被调用,我这样做:

public void onNFCReaderTaskCompleted(Long time, String value) {
    Intent newIntent = new Intent(this.getApplicationContext(), NFCResultActivity.class);
    newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    newIntent.putExtra(Globals.KEY_NFC, true);
    newIntent.putExtra(Globals.KEY_TIME, time);
    newIntent.putExtra(Globals.KEY_VALUE, value);

    this.startActivity(newIntent);
    this.finish();
}

第一次发现 NFC 标签时很好。我现在想要的是不要 总是启动一个新的 NFCResultActivity。当 NFCResultActivity 处于活动状态时,我只是 想要调用该 NFCResultActivity 的方法来设置新数据而不是完全 创建一个新的活动。

我怎样才能做到这一点?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    我以不同的方式解决了它。

    我删除了我的 NFCActivity 并更改了我的 NFCResultActivity 以进行意图监听:

    <activity
            android:name="controller.NFCResultActivity"
            android:label="@string/title_activity_nfcresult" 
            android:launchMode="singleTask" >
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />
        </activity>
    

    我现在在我的 NFCResultActivity 中启动 NFCReaderTask 并添加了这个方法:

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    
        this.setIntent(intent);
        this.resolveIntent(intent);
    }
    

    在不创建新活动的情况下更新我的数据。

    【讨论】:

      【解决方案2】:

      只需编辑这一行。

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

       <activity
      android:name="controller.NFCActivity" 
      android:launchMode="singleTop" >
      

      【讨论】:

      • 嗯,这实际上使情况变得更糟。首先它不会假装一遍又一遍地开始活动,其次当我手动启动我的 NFCResultActivity 并且放置一个标签时,我在顶部有两个 NFCResultActivities。
      • 移除这个标志 Intent.FLAG_ACTIVITY_CLEAR_TOP|
      • 在清单中使用它并从意图中删除标志
      猜你喜欢
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多