android - Starting a new activity -
i have tabactivity , , on click of each tab new activity invoked . in each activity have option tap nfc card , value. problem when tapping card on phone , new activity invoked without tabactivity parent. code runs fine in non tabactivity .
this code .
adapter = nfcadapter.getdefaultadapter(this); pendingintent = pendingintent.getactivity(this, 0, new intent(this, getclass()).addflags(intent.flag_activity_single_top), 0); intentfilter tagdetected = new intentfilter(nfcadapter.action_tag_discovered); tagdetected.addcategory(intent.category_default); writetagfilters = new intentfilter[] { tagdetected }; ctx=this drop down option select tag if(position == 2){ mmsisdn_layout.setvisibility(view.gone); mnfc_layout.setvisibility(view.visible); if(!adapter.isenabled()){ toast.maketext(getapplicationcontext(), "please activate nfc , press return application!", toast.length_long).show(); startactivity(new intent(android.provider.settings.action_wireless_settings)); } else{ mcustomizedialog = new customizedialog(this); mcustomizedialog.settitle("nfc"); mcustomizedialog.setmessage("please tap nfc card"); mcustomizedialog.progressbar.setvisibility(view.invisible); mcustomizedialog.cancelbutton.setvisibility(view.gone); mcustomizedialog.okbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(mytag==null){ mcustomizedialog.dismiss(); }else{ // medtnfc.settext(nfcidvalue); mcustomizedialog.dismiss(); } } }); mcustomizedialog.show(); } } /** * * @param text * @param tag * @throws ioexception * @throws formatexception */ private void write(string text, tag tag) throws ioexception, formatexception { ndefrecord[] records = { createrecord(text) }; ndefmessage message = new ndefmessage(records); // instance of ndef tag. ndef ndef = ndef.get(tag); // enable i/o ndef.connect(); // write message ndef.writendefmessage(message); // close connection ndef.close(); } /** * * @param text * @return * @throws unsupportedencodingexception */ private ndefrecord createrecord(string text) throws unsupportedencodingexception { string lang = "en"; byte[] textbytes = text.getbytes(); byte[] langbytes = lang.getbytes("us-ascii"); int langlength = langbytes.length; int textlength = textbytes.length; byte[] payload = new byte[1 + langlength + textlength]; // set status byte (see ndef spec actual bits) payload[0] = (byte) langlength; // copy langbytes , textbytes payload system.arraycopy(langbytes, 0, payload, 1, langlength); system.arraycopy(textbytes, 0, payload, 1 + langlength, textlength); ndefrecord recordnfc = new ndefrecord(ndefrecord.tnf_unknown, ndefrecord.rtd_text, new byte[0], payload); return recordnfc; } /** * */ @override protected void onnewintent(intent intent){ if(nfcadapter.action_tag_discovered.equals(intent.getaction())){ mytag = intent.getparcelableextra(nfcadapter.extra_tag); log.d("nfc intent", intent.getaction()); // mytag = intent.getparcelableextra(nfcadapter.extra_tag); // system.out.println(myta); log.d("nfc tag", mytag.tostring()); // log.d("nfc tag",bytestohex(mytag.getid()).trim()); nfcidvalue = convertbytetohex.bytestohex(mytag.getid()).trim(); medtnfc.settext(nfcidvalue); // nfcidvalue =""; toast.maketext(this,"tap successful", toast.length_long ).show(); } } @override public void onpause(){ super.onpause(); writemodeoff(); } /*@override public void onresume(){ super.onresume(); }*/ /** * */ private void writemodeon(){ if(android.os.build.version.sdk_int >= 16){ writemode = true; adapter.enableforegrounddispatch(this, pendingintent, writetagfilters, null); } } /** * */ private void writemodeoff(){ if(android.os.build.version.sdk_int >= 16){ writemode = false; adapter.disableforegrounddispatch(this); } } this code works fine dont understand why new activity invoked when using in tab activity.
try handling nfc intents in tabactivity instead. better yet (from android ui design perspective) redesign app , use fragments in viewpager within single activity (with actionbar). never run problem.
Comments
Post a Comment