muoki_D
muoki_D

Reputation: 409

Start new activity after writing to NFC tag

I am trying to start a new activity after a write operation to an NFC tag has been completed. I tried using a handler but it does not work, the tag gets writen to successfully but the handler doesnt launch the activity that it should launch after the write operation

private void formatTag(Tag tag, NdefMessage ndefMessage)
    {
        NdefFormatable ndefFormatable = NdefFormatable.get(tag);

        if (ndefFormatable == null)
        {
            Toast.makeText(this, "Tag is not NDEF formatable", Toast.LENGTH_LONG).show();
            return;
        }

        try
        {
            ndefFormatable.connect();
            ndefFormatable.format(ndefMessage);
            ndefFormatable.close();
            Toast.makeText(this, "Tag has be written successfully!", Toast.LENGTH_LONG).show();
            writeHandler.sendEmptyMessage(0);

        }
        catch (Exception e)
        {
            Log.e("formatTag: ", e.getMessage());
        }


    }

    private Handler writeHandler = new Handler() {
        public void handleMessage(Message msg) {
            Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class);
            startActivityForResult(nextActivity, 0);
            WriteCardActivity.this.finish();
        }
    };

here is the manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.loyalty.cardplanet.membershipcard" >

    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <uses-feature
        android:name="android.hardware.nfc"
        android:required="false" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RegisterActivity"
            android:label="@string/title_activity_register" >
        </activity>
        <activity
            android:name=".RedeemActivity"
            android:label="@string/title_activity_redeem" >
        </activity>
        <activity
            android:name=".PurchaseActivity"
            android:label="@string/title_activity_purchase" >
        </activity>
        <activity
            android:name=".ResetPinActivity"
            android:label="@string/title_activity_reset_pin" >
        </activity>
        <activity
            android:name=".WriteCardActivity"
            android:label="@string/title_activity_write_card" >
        </activity>
    </application>

</manifest>

Upvotes: 0

Views: 365

Answers (3)

This code is worked for me. And don't call WriteCardActivity from MainActivity's onCreate, onResume or onPause. Otherwise WriteCardActivity will start again.

private void formatTag(Tag tag, NdefMessage ndefMessage)
{
    NdefFormatable ndefFormatable = NdefFormatable.get(tag);

    if (ndefFormatable == null)
    {
        Toast.makeText(this, "Tag is not NDEF formatable", Toast.LENGTH_LONG).show();
        return;
    }

    try
    {
        ndefFormatable.connect();
        ndefFormatable.format(ndefMessage);
        ndefFormatable.close();
        Toast.makeText(this, "Tag has be written successfully!", Toast.LENGTH_LONG).show();

        Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class);
        startActivityForResult(nextActivity, 0);
        WriteCardActivity.this.finish();

    }
    catch (Exception e)
    {
        Log.e("formatTag: ", e.getMessage());
    }


}

Upvotes: 0

muoki_D
muoki_D

Reputation: 409

I figured it out. I decided to use an AlertDialog instead of a Handler so I removed the Handler part and added this to onNewIntent

@Override
    protected void onNewIntent(Intent intent) {


        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        NdefMessage ndefMessage = createNdefMessage(account+"");

        writeNdefMessage(tag, ndefMessage);

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(WriteCardActivity.this);
        alertDialog.setMessage("Card Written Successfully!");

        alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent intent = new Intent(WriteCardActivity.this, MainActivity.class);
                Bundle b = new Bundle();
                b.putBoolean("new_window", true); //sets new window
                intent.putExtras(b);
                startActivity(intent);
            }
        });


        alertDialog.create();
        alertDialog.show();

        super.onNewIntent(intent);
    }

Upvotes: 0

Gaskoin
Gaskoin

Reputation: 2485

You have an IntentFilter on your MainActivity so Intent should match activity's IntentFilter.

So you should start your activity like this:

Intent nextActivity = new Intent(WriteCardActivity.this, MainActivity.class);
nextActivity.setAction(Intent.ACTION_MAIN);
nextActivity.addCategory(Intent.CATEGORY_LAUNCHER);

startActivity(nextActivity);
WriteCardActivity.this.finish();

Upvotes: 2

Related Questions