我的問題是關於後退按鈕和後臺堆棧,我有一個閱讀nfc標籤的應用程序,所以我先啓動Activity A,然後進入活動B,在manifest中聲明SingleTop。 我將手機標記爲標籤,它讀取標籤,此時一切正常。閱讀NFC標籤和返回按鈕
如果我按後退按鈕,它可以追溯到活動B,而不是去活性的,然後如果我按了後退按鈕它再次進入到活動A.
像這樣:
A-> B->閱讀標籤 - > B->按下後退按鈕 - > B->按下後退按鈕 - > A->按下後退按鈕 - >關閉應用程序。
,我想是這樣的:
A-> B->閱讀Tags-> B->按返回按鈕 - > A->按後退按鈕 - >關閉應用程序。
我只想要B的一個實例。 我試過單個任務,但問題是我點擊了應用程序圖標,並且活動b已啓動,但讀取標記的意圖被保留。
public class B extends Activity {
private static final String KINVEY_KEY = YOUR_APP_KEY;
private static final String KINVEY_SECRET_KEY = 'YOUR_APP_SECRET_KEY';
private KCSClient kinveyClient;
private NfcAdapter mNfcAdapter;
private Button mEnableWriteButton;
private EditText mTextField;
private ProgressBar mProgressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tags);
mTextField = (EditText) findViewById(R.id.text_field);
mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
mProgressBar.setVisibility(View.GONE);
mEnableWriteButton = (Button) findViewById(R.id.enable_write_button);
mEnableWriteButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setTagWriteReady(!isWriteReady);
mProgressBar.setVisibility(isWriteReady ? View.VISIBLE : View.GONE);
}
});
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
Toast.makeText(this, 'Sorry, NFC is not available on this device', Toast.LENGTH_SHORT).show();
finish();
}
// Initialize Kinvey
KinveySettings settings = new KinveySettings(KINVEY_KEY, KINVEY_SECRET_KEY);
kinveyClient = KCSClient.getInstance(this.getApplicationContext(), settings);
}
private boolean isWriteReady = false;
public void setTagWriteReady(boolean isWriteReady) {
this.isWriteReady = isWriteReady;
if (isWriteReady) {
IntentFilter[] writeTagFilters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED) };
mNfcAdapter.enableForegroundDispatch(TagsActivity.this, NfcUtils.getPendingIntent(TagsActivity.this),
writeTagFilters, null);
} else {
// Disable dispatch if not writing tags
mNfcAdapter.disableForegroundDispatch(TagsActivity.this);
}
mProgressBar.setVisibility(isWriteReady ? View.VISIBLE : View.GONE);
}
@Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}
@Override
public void onResume() {
super.onResume();
if (isWriteReady && NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
processWriteIntent(getIntent());
} else if (!isWriteReady
&& (NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction()) || NfcAdapter.ACTION_NDEF_DISCOVERED
.equals(getIntent().getAction()))) {
processReadIntent(getIntent());
}
}
private static final String MIME_TYPE = 'application/com.tapped.nfc.tag';
public void processWriteIntent(Intent intent) {
if (isWriteReady && NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
Tag detectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
String tagWriteMessage = mTextField.getText().toString();
byte[] payload = new String(tagWriteMessage).getBytes();
if (detectedTag != null && NfcUtils.writeTag(
NfcUtils.createMessage(MIME_TYPE, payload), detectedTag)) {
Toast.makeText(this, "Wrote '" + tagWriteMessage + "' to a tag!",
Toast.LENGTH_LONG).show();
setTagWriteReady(false);
} else {
Toast.makeText(this, "Write failed. Please try again.", Toast.LENGTH_LONG).show();
}
}
}
public void processReadIntent(Intent intent) {
List<NdefMessage> intentMessages = NfcUtils.getMessagesFromIntent(intent);
List<String> payloadStrings = new ArrayList<String>(intentMessages.size());
for (NdefMessage message : intentMessages) {
for (NdefRecord record : message.getRecords()) {
byte[] payload = record.getPayload();
String payloadString = new String(payload);
if (!TextUtils.isEmpty(payloadString))
payloadStrings.add(payloadString);
}
}
if (!payloadStrings.isEmpty()) {
String content = TextUtils.join(",", payloadStrings);
Toast.makeText(TagsActivity.this, "Read from tag: " + content,
Toast.LENGTH_LONG).show();
saveTag(content);
}
}
private void saveTag(String tagMessage){
TagReadEntity tag = new TagReadEntity(UUID.randomUUID().toString(),
tagMessage, System.currentTimeMillis());
kinveyClient.mappeddata("tags").save(tag, new ScalarCallback<TagReadEntity>() {
@Override
public void onSuccess(TagReadEntity tag) {
Log.i("NFC Demo", "Saved tag!");
}
@Override
public void onFailure(Throwable e) {
Log.e("NFC Demo", "Error saving tag", e);
}
});
}
}
和清單:
<activity
android:name=".TagsActivity"
android:label="@string/title_activity_tags"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/com.tapped.nfc.tag" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/com.tapped.nfc.tag" />
</intent-filter>
</activity>
你是否重寫了'onBackPressed()'方法? – dumbfingers
沒有。該方法不存在於我的代碼中。 –
請參閱http://developer.android.com/reference/android/app/Activity.html#onBackPressed()並在您的活動B中重寫此方法以通過代碼完成活動B. – dumbfingers