Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:02:38+00:00 2026-06-14T21:02:38+00:00

When I scan my NFC tag, my application fires up and does what it

  • 0

When I scan my NFC tag, my application fires up and does what it have to do , but the tabhost goes missing. The mainactivity.java is where I initialize the tabhost with 3 tabs and this following first part of the code is one of the tab in which I utilize the NFC reading function. Is this because of the pendingintent? I don’t really know how to fix it, do I have to implement the 3 tabs in every activity layout?

My code:

package com.example.ponpon;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.PatternMatcher;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

@TargetApi(10)
public class CouponManager extends Activity {

        private static final String TAG = "NFCReadTag";
        private NfcAdapter mNfcAdapter;
        private IntentFilter[] mNdefExchangeFilters;
        private PendingIntent mNfcPendingIntent;
        public static final String PREF_FILE_NAME = "PrefFile";

        private int[] images = new int[11];

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.coupon_layout);

            //List of images
            images[0]=R.drawable.cp0;
            images[1]=R.drawable.cp1;
            images[2]=R.drawable.cp2;
            images[3]=R.drawable.cp3;
            images[4]=R.drawable.cp4;
            images[5]=R.drawable.cp5;
            images[6]=R.drawable.cp6;
            images[7]=R.drawable.cp7;
            images[8]=R.drawable.cp8;
            images[9]=R.drawable.cp9;
            images[10]=R.drawable.cp10;

            //Restore preferences 
            SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
            int storedPreference = preferences.getInt("storedInt", 0);

            //Image to use depending on coupon collected
            final ImageView img = (ImageView)findViewById(R.id.imageView1);

            if(storedPreference!=10)
            {
                img.setImageResource(images[storedPreference]);
                img.invalidate();
            }
            else
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setCancelable(false);
                builder.setTitle("Coupon Redemption");
                builder.setMessage("Redeem Your Coupon?");
                builder.setInverseBackgroundForced(true);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                        SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putInt("storedInt", 0); // value to store
                        editor.commit();    
                        img.setImageResource(images[0]);
                        img.invalidate();
                    }
                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        img.setImageResource(images[10]);
                        img.invalidate();
                    }
                });
                builder.show();
            }


            //Check and send Intent from NFC tag discovered
            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

            mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                    /*| Intent.FLAG_ACTIVITY_CLEAR_TOP*/), 0);


            IntentFilter coupontag = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
            coupontag.addDataScheme("http");
            coupontag.addDataAuthority("www.ichatime.com", null);
            coupontag.addDataPath(".*", PatternMatcher.PATTERN_SIMPLE_GLOB);

            mNdefExchangeFilters = new IntentFilter[] { coupontag };

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

        @Override
        protected void onResume() {
            super.onResume();
            if(mNfcAdapter != null) {
                mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent,
                    mNdefExchangeFilters, null);
            } else {
                Toast.makeText(getApplicationContext(), "Sorry, No NFC Adapter found.", Toast.LENGTH_SHORT).show();
            }


        }

        @Override
        protected void onPause() {
            super.onPause();
            if(mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this);
            SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
            int storedPreference = preferences.getInt("storedInt", 0);

            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt("storedInt", storedPreference); // value to store
            editor.commit();    
        }

        @Override
        protected void onStop() {
            super.onStop();

            // We need an Editor object to make preference changes.
              // All objects are from android.context.Context
                SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
                int storedPreference = preferences.getInt("storedInt", 0);

                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("storedInt", storedPreference); // value to store
                editor.commit();    
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();

            // We need an Editor object to make preference changes.
              // All objects are from android.context.Context
                SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
                int storedPreference = preferences.getInt("storedInt", 0);

                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("storedInt", storedPreference); // value to store
                editor.commit();    
        }

        @Override
           public void onBackPressed() {
            SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
            int storedPreference = preferences.getInt("storedInt", 0);

            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt("storedInt", storedPreference); // value to store
            editor.commit();    
               super.onBackPressed();
           }

        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);      
            SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
            int storedPreference = preferences.getInt("storedInt", 0);

            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
                NdefMessage[] messages = null;
                Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                if (rawMsgs != null) {
                    messages = new NdefMessage[rawMsgs.length];
                    for (int i = 0; i < rawMsgs.length; i++) {
                        messages[i] = (NdefMessage) rawMsgs[i];
                    }
                }
                if(messages[0] != null) {
                    String result="";
                    byte[] payload = messages[0].getRecords()[0].getPayload();
                    // this assumes that we get back am SOH followed by host/code
                    for (int b = 1; b<payload.length; b++) { // skip SOH
                        result += (char) payload[b];
                    }
                    if (result.contains("ichatime.com"))
                        {
                        final ImageView img = (ImageView)findViewById(R.id.imageView1);


                        /*if (storedPreference!=10)
                        {
                            Toast.makeText(getApplicationContext(), "Coupon collected!", Toast.LENGTH_SHORT).show();
                            storedPreference++;
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putInt("storedInt", storedPreference);
                            img.setImageResource(images[storedPreference]);
                            img.invalidate();
                        }*/
                            if (storedPreference==10)
                            {
                                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                                builder.setCancelable(false);
                                builder.setTitle("Redeem Your Coupon?");
                                builder.setInverseBackgroundForced(true);
                                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        dialog.dismiss();
                                        SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
                                        SharedPreferences.Editor editor = preferences.edit();
                                        editor.putInt("storedInt", 0); // value to store
                                        editor.commit();    
                                        img.setImageResource(images[0]);
                                        img.invalidate();
                                    }
                                });
                                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        img.setImageResource(images[10]);
                                        img.invalidate();
                                    }
                                });
                                builder.show();
                            }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "Coupon collected!", Toast.LENGTH_SHORT).show();
                            storedPreference++;
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putInt("storedInt", storedPreference);
                            editor.commit();
                            img.setImageResource(images[storedPreference]);
                            img.invalidate();
                        }}
                    else 
                    {
                        Toast.makeText(getApplicationContext(), "Wrong tag detected!", Toast.LENGTH_SHORT).show();
                    }
                    //Debugging Mode to see what is contained in the tags.
            //          Toast.makeText(getApplicationContext(), "Tag Contains " + result, Toast.LENGTH_SHORT).show();
                }
            }
        }

    }

and also, my main activity:

package com.example.ponpon;

import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost tabHost = getTabHost();

        TabSpec spec1=tabHost.newTabSpec("Coupon Manager");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("Coupons");
        Intent CouponsIntent = new Intent(this, CouponManager.class);
        spec1.setContent(CouponsIntent);

        TabSpec spec2=tabHost.newTabSpec("Help");
        spec2.setIndicator("Help");
        spec2.setContent(R.id.tab2);
        Intent SettingsIntent = new Intent(this, Settings.class);
        spec2.setContent(SettingsIntent);

        TabSpec spec3=tabHost.newTabSpec("Write[Vendor]");
        spec3.setIndicator("Write\n[Vendor]");
        spec3.setContent(R.id.tab3);
        Intent HelpIntent = new Intent(this, HelpActivity.class);
        spec3.setContent(HelpIntent);

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:02:39+00:00Added an answer on June 14, 2026 at 9:02 pm

    Take a close look at your logic: you have

    if (storedPreference!=10) {
        ...stuff... (including storedPreference++)
    }
    if (storedPreference==10) {
        ...stuff...
    } else {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("storedInt", 10);
        img.setImageResource(images[10]);
        img.invalidate();
    }
    

    You should consider replacing the contents of the else block with the storedPreference!=10 block, then getting rid of that first block. All you need is if storedPreference==10 and else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perhaps I'm missing something but when I scan an NFC tag via a galaxy
I save the AAR on my NFC-Tag. If I scan the TAG with the
I am trying to scan memory of a 3rd party application. I have already
In my app, I only have one activity with an NFC intent but it
How can I scan/record IP requests from a specific application or task? Could I
How do you scan a file with java that isn't in the directory the
After a quick scan of related questions on SO, I have deduced that there's
I have a scan results of some 50 vlans it was easy when i
I'm trying to launch a specific activity when my phone scans an NFC Tag.
I want to scan a website using jQuery, but the ID is constantly changing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.