I have an application that reads NFC tags (when main button in MainActivity is clicked), then displays the result in a modal. It works well but know, what I'd like to do is create a fragment to use ViewPager (to have nice transition on my future fragments).
But now, at first, I can't hit my main button. Then, when I scan a tag, it put me on other view (or i don't know what, I don't totally understand) and it displays me the result in a modal. At this moment, I'm on the right side of my application with the same view.
After this, I can hit my main button and scan tags, but, i'm on the wrong fragment or something like that and I can only go to the left
here is my code project : https://github.com/elgrusko/nfc_stackoverflow
Or my main code :
package com.example.poissonpane;
public class MainActivity extends FragmentActivity {
private static final int NUM_PAGES = 2;
private ViewPager mPager;
private PagerAdapter pagerAdapter;
public static int display, result, detect = 0;
private NfcAdapter nfcAdapter;
private ImageButton nfcButton;
private PendingIntent pendingIntent;
public static IntentIntegrator integrator;
public ScreenSlidePageFragment scan_fragment;
public static String link = null;
public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Log.d("TOTO", Integer.toString(position));
switch (position) {
case 0:
return new ScreenSlidePageFragment();
case 1:
return new SecondFragment();
default:
return new ScreenSlidePageFragment();
}
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
public void onResume() {
super.onResume();
Log.d("TOTO", "onResume() main activity");
Intent intent = getIntent(); // Recuperer l'intent actuel
NdefMessage[] msgs;
// If a NFC tag has been detected once app was closed
if (detect == 1 && (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()))) { // si l'application a ete lancee via une detection NFC du device (ACTION_NDEF_DISCOVERED, voir AndroidManifest)
Parcelable[] rawMessages =
intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
if (rawMessages != null) { // if NDEF in scan
display = 1;
resolveIntent(intent); // parser NFC scan
} else {
byte[] empty = new byte[0];
byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] payload = NdefMessageParser.dumpTagData(tag).getBytes();
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload);
NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
msgs = new NdefMessage[] {msg};
detect = 0;
scan_fragment.getResult(msgs);
}
}
NfcAdapter nfcAdapter2 = this.nfcAdapter;
if (nfcAdapter2 != null) {
if (!nfcAdapter2.isEnabled()) {
showWirelessSettings();
}
this.nfcAdapter.enableForegroundDispatch(this, this.pendingIntent, (IntentFilter[]) null, (String[][]) null);
}
}
private void showWirelessSettings() { // ask user to enable NFC on his devices
Toast.makeText(this, "Veuillez activer le service NFC", Toast.LENGTH_SHORT).show();
startActivity(new Intent("android.settings.WIRELESS_SETTINGS"));
}
public void onPause() {
Log.d("TOTO", "onPause() MainActivity");
detect = 0;
super.onPause();
}
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
if (detect != 1) {
resolveIntent(intent);
}
}
private void resolveIntent(Intent intent) { // DETECT NFC & PARSING NDEF
NdefMessage[] msgs;
String action = intent.getAction();
Log.d("Infos", getIntent().toString());
Log.d("Infos", "display = " + display);
if (display != 1) {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
nfcButton.startAnimation(shake);
Toast.makeText(this, "Veuillez lancer un scan", Toast.LENGTH_LONG).show();
return;
}
else if ("android.nfc.action.TAG_DISCOVERED".equals(action) || "android.nfc.action.TECH_DISCOVERED".equals(action) || "android.nfc.action.NDEF_DISCOVERED".equals(action)) {
result = 1;
Parcelable[] rawMsgs = intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
} else {
byte[] empty = new byte[0];
byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] payload = NdefMessageParser.dumpTagData(tag).getBytes();
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload);
NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
msgs = new NdefMessage[] {msg};
}
if (display == 1) {
scan_fragment.getResult(msgs);
}
}
}
public void ScanResultModal(String link, StringBuilder tmp_builder) { // we don't care for the moment
Log.d("Toto", "ScanResultModal");
/* Intent i = new Intent(MainActivity.this, RightActivity.class);
link = tmp_builder.toString();
i.putExtra("url", link);
startActivity(i);*/
}
}
package com.example.poissonpane;
import java.util.List;
import static com.example.poissonpane.MainActivity.link;
public class ScreenSlidePageFragment extends Fragment {
public MainActivity mainActivity;
private ImageButton nfcButton;
ObjectAnimator anim;
ObjectAnimator anim2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
mainActivity = (MainActivity) getActivity();
Log.d("TOTO", "onCreateView() Fragment");
MainActivity.display = 1;
anim = ObjectAnimator.ofFloat(nfcButton, "scaleX", new float[]{0.9f});
anim2 = ObjectAnimator.ofFloat(nfcButton, "scaleY", new float[]{0.9f});
nfcButton = (ImageButton) rootView.findViewById(R.id.nfc_button);
nfcButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainActivity.display = 1;
manageBlinkEffect();
}
});
return rootView;
}
public void manageBlinkEffect() { // Just to make grow and shrink the Button while scanning // ANIMATION DU LOGO SENZU SUITE A UN CLIC
anim.setDuration(1000);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setRepeatCount(9);
anim.start();
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationCancel(animation);
if (MainActivity.result != 1){
Animation shake = AnimationUtils.loadAnimation(getContext(), R.anim.shake);
Toast.makeText(getContext(), "Aucun badge n'a été détécté !", Toast.LENGTH_SHORT).show();
nfcButton.startAnimation(shake);
}
}
});
anim2.setDuration(1000);
anim2.setRepeatMode(ValueAnimator.REVERSE);
anim2.setRepeatCount(9);
anim2.start();
}
public void getResult(NdefMessage[] msgs) { // MODAL RESULT SCAN NFC
if (msgs != null && msgs.length != 0) {
final StringBuilder tmp_builder = new StringBuilder();
List<ParsedNdefRecord> records = NdefMessageParser.parse(msgs[0]);
int size = records.size();
for (int i = 0; i < size; i++) {
tmp_builder.append(records.get(i).str());
tmp_builder.append("\n");
}
System.out.println(tmp_builder.toString());
this.anim.cancel();
this.anim2.cancel();
MainActivity.display = 0;
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Resultat du scan :");
builder.setNeutralButton("Visiter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
((MainActivity)getActivity()).ScanResultModal(link, tmp_builder);
}
});
builder.setNegativeButton("Copier", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Texte copié", link);
clipboard.setPrimaryClip(clip);
}
});
builder.setPositiveButton("Fermer", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}
});
builder.setMessage(tmp_builder.toString());
builder.create().show();
}
}
}
Do you see something wrong in my fragment or something else ?
Sorry for the explication. It's really hard to explain
thanks !
User contributions licensed under CC BY-SA 3.0