android-NFC碎片,onNewIntent isn';我从另一个活动回来后没有打电话
我有一个使用NFC的应用程序。我有一个扩展BaseActivity的MainActivity。在BaseActivity的“onCreate”方法中,我初始化nfcandroid-NFC碎片,onNewIntent isn';我从另一个活动回来后没有打电话,android,fragment,nfc,onnewintent,Android,Fragment,Nfc,Onnewintent,我有一个使用NFC的应用程序。我有一个扩展BaseActivity的MainActivity。在BaseActivity的“onCreate”方法中,我初始化nfc ---- Base Activity ---- .... private void initializeNfc() { NfcManager manager = (NfcManager) getSystemService(NFC_SERVICE); nfcAdapter = manager.getDe
---- Base Activity ----
....
private void initializeNfc() {
NfcManager manager = (NfcManager) getSystemService(NFC_SERVICE);
nfcAdapter = manager.getDefaultAdapter();
IntentFilter filterNdef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
filterNdef.addDataScheme(CipherClient.nfcDataScheme());
filterNdef.addDataAuthority(CipherClient.nfcDataAuthority(), null);
filterNdef.addDataPath(CipherClient.nfcDataPath(), PatternMatcher.PATTERN_SIMPLE_GLOB);
} catch (Exception e) {
Log.e(TAG, "Errore", e);
return;
}
intents = new IntentFilter[]{filterNdef};
pending = PendingIntent.getActivity(this, 0, new Intent(getApplicationContext(), getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
techs = new String[][]{new String[]{NfcF.class.getName()}};
}
---- MainActivity ----
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.i(TAG, "onPageSelected - position: "+position);
currentTabPosition = position;
if(position == 0) {
// I am in the NFC tab. So I set true the flag
if (verifyNfc()) {
nfcON = true;
}
}
else{
// I'm not on the nfc tab, so I set false the flag
nfcON = false;
enableForegroundDispatch();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
private boolean verifyNfc() {
boolean nfcEnabled = nfcAdapter.isEnabled();
if (!nfcEnabled) {
Log.i(TAG, "NFC not activated");
// NFC disabled
Toast....
}
return nfcEnabled;
}
在MainActivity中,我有一个带有4个选项卡的滑块,每个选项卡中都有一个片段。我可以使用nfc的片段只有数字0(NfcFragment)
第一次启动MainActivity时,调用“onCreate”方法并显示第一个片段(FirstFragment)。如果我不在nfc区,我接近nfc标签,我会得到一个祝酒词,上面写着“到nfc区去使用nfc”
如果我移动到nfc片段并接近nfc标记,将调用“onNewIntent”方法并打开一个新活动(MyNewActivity)。在MyNewActivity中,我按下back键,调用“onBackPressed”,然后执行“finish()
从MyNewActivity返回,我发现自己再次进入了MainActivity(它已经实例化,因此不是“onCreate”,而是“onResume”)
现在,如果我去看一个不是NFCFFragment的片段,它应该会显示一个祝酒词,对吗?相反,在任何片段中,如果我接近nfc标记,MyNewActivity将被打开。问题是不再调用onNewIntent方法,而是绕过它
我在“onPause”中使用了disableForegroundDispatch方法,在“onResume”中使用了正确启用foregrounddispatch方法。我还使用了FLAG_ACTIVITY_SINGLE_TOP、FLAG_ACTIVITY_SINGLE_TASK等标志
如果我在MainActivity的onResume方法中重新初始化nfc,或者在MyNewActivity的“onBackPressed”方法中执行startActivity(这个,MainActivity.class),则可以解决此行为,因为它被称为onCreate方法,并且创建了该活动的新实例。但这是一个解决办法,我不想反复初始化nfc
---- MyNewActivity ----
// Solution 1 to which I do not want to resort because it is heavy to load 4 tabs together in the main activity
@Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
finish();
}
---- MainActivity ----
// Solution 2 that I don't want to use because onResume is called more than once
@Override
protected void onResume() {
Log.i(TAG, "onResume");
super.onResume();
// NEW NEW NEW
initializeNFC(); // <----------------------------
enableForegroundDispatch();
if(currentTabPosition == 0)
nfcON = true;
else
nfcON = false;
}
----我的新活动----
//我不想求助于解决方案1,因为在主活动中同时加载4个选项卡非常繁重
@凌驾
public void onBackPressed(){
super.onBackPressed();
意图i=新意图(此,MainActivity.class);
星触觉(i);
完成();
}
----主要活动----
//我不想使用的解决方案2,因为onResume被多次调用
@凌驾
受保护的void onResume(){
Log.i(标签“onResume”);
super.onResume();
//新的
initializeNFC();//建议1:请输入代码,这样我们就不必使用我们的想象力建议1:请输入代码,这样我们就不必使用我们的想象力
---- MyNewActivity ----
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
---- MainActivity ----
@Override
protected void onResume() {
Log.i(TAG, "onResume");
super.onResume();
enableForegroundDispatch();
if(currentTabPosition == 0)
nfcON = true;
else
nfcON = false;
}
public void enableForegroundDispatch(){
Log.i(TAG, "enableForegroundDispatch");
try {
if (nfcAdapter != null) {
if (nfcAdapter.isEnabled())
nfcAdapter.enableForegroundDispatch(this, pending, intents, techs);
}
}
catch (Exception e){
Toast...
}
}
---- MyNewActivity ----
// Solution 1 to which I do not want to resort because it is heavy to load 4 tabs together in the main activity
@Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
finish();
}
---- MainActivity ----
// Solution 2 that I don't want to use because onResume is called more than once
@Override
protected void onResume() {
Log.i(TAG, "onResume");
super.onResume();
// NEW NEW NEW
initializeNFC(); // <----------------------------
enableForegroundDispatch();
if(currentTabPosition == 0)
nfcON = true;
else
nfcON = false;
}