I am working on a project where I have 3 tabs. The first tab is used to write information to NFC tag where the other 2 tabs are used to read the information from the NFC tag. However, I am facing a problem with OnNewIntent() method. As I understood correctly from reading online is that this method is used only in activities and not in fragments. I have looked at similar questions and answers but I don't fully understand what needs to be done to avoid this issue in my situation.
Here is my code for the first tab where I write some data to NFC tag:
public class Home extends Fragment {
NfcAdapter nfcAdapter;
Button writebtn;
Tag tag;
EditText txtName, txtCountry, txtID;
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view,savedInstanceState);
nfcAdapter = NfcAdapter.getDefaultAdapter(getContext());
txtName = (EditText)view.findViewById(R.id.personName);
txtCountry= (EditText)view.findViewById(R.id.personCountry);
txtID= (EditText)view.findViewById(R.id.personID);
writebtn=(Button)view.findViewById(R.id.nfcWriteBtn);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.home, container,false);
return v;
}
@Override
public void onResume() {
super.onResume();
if(nfcAdapter !=null){
enableForegroundDispatchSystem();
}
}
@Override
public void onPause() {
super.onPause();
disableForegroundDispatchSystem();
}
protected void onNewIntent(final Intent intent) {
// super.onNewIntent(intent);
getActivity().getIntent();
if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
Toast.makeText(getContext(), "NFC tag discovered!", Toast.LENGTH_SHORT).show();
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
writebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NdefMessage ndefMessage = createNdefMessage(txtName.getText().toString(), txtCountry.getText().toString(),txtID.getText().toString());
writeNdefMessage(tag, ndefMessage);
}
});
}
}
onNewIntent(Intent intent)
belongs to Activity
you cannot have it in fragment. What you can do is pass the data to your fragment whenever onNewIntent(Intent intent)
get called in Activity
. To achieve this You need to override onNewIntent()
in Activity and notify the fragments about intent . Do not use getIntent()
use the intent
which is in argument.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Notify fragment about the new intent
}
And your Fragments method should look like .
protected void onNewIntent( Intent intent) {
if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
Toast.makeText(getContext(), "NFC tag discovered!", Toast.LENGTH_SHORT).show();
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
writebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NdefMessage ndefMessage = createNdefMessage(txtName.getText().toString(), txtCountry.getText().toString(),txtID.getText().toString());
writeNdefMessage(tag, ndefMessage);
}
});
}