I've developed a simple NFC application that reads and displays the data(NDEF Records) present in the NFC tag. I modified the Manifest file so that whenever a tag gets tapped, my app gets opened.
Now the problem is when I manually open the app and check the multitasking screen, it displays my app name i.e NFCReader but when the app gets opened itself when a tag comes in contact with the device, my app gets opened but when I check the multitasking screen, it doesn't display my app name. Instead it displays NFC Service. What should I do to overcome this issue?
I assume that you have set your activity declaration, in your manifest, to something like this:
<activity
android:name=".packagename.MainActivity"
android:alwaysRetainTaskState="true"
android:launchMode="singleTask"
android:theme="#style/AppTheme.Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="domain.com"
android:scheme="http" />
</intent-filter>
</activity>
I think the answer to your question is the following line of code that you forgot to add:
android:launchMode="singleTask"
I invite you to read about launch modes here and here.
Please note: This answer is based on the use of Reader mode API, otherwise check this answer.
Hope this helps!
Related
I am using Branch IO in my application and as per their documentation, I am using android:launchMode="singleTask" for my activity.
Here is some code snippet of my AndroidManifest.
<activity
android:name=".SplashScreen"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data
android:host="open"
android:scheme="com.package.name" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<!-- Branch App Links (optional) -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="app.link"
android:scheme="https" />
<!-- <data android:scheme="https" android:host="example-alternate.app.link" /> -->
</intent-filter>
</activity>
Everything is working fine except when I press Home button while using app and tap on App/Launcher Icon, onCreate method of Splash screen activity is called which makes it looks like app is launched from beginning. However, if I press home button while using an app and open it from recent apps, onCreate is not called and everything works perfectly.
How do I make consistent app behavior when brought to foreground from recent apps and App/Launcher icon?
I tried removing singleTask launch mode which makes launch perfect from App/Launcher icon and recent apps but when tapped on branch IO link, the new instance of an app is created. I can understand that to overcome this problem only they are asking to put singleTask in launch mode.
I have checked for this scenario in many apps which are using deep links and they do not have this problem!
I must be doing something wrong which I cannot see.
Is something is missing or implementation is wrong?
Solution to this problem is moving to Single activity architecture.
As splash activity is singleTask, onStart method of it will be called each time when the app is brought to foreground from launcher icon. One has to write routing logic in onStart method, when another activity is started from here, app state will lose.
If a single activity is there, this problem will not be there. However one will have to deal with nasty fragment backstacks. Not an easy but correct approach.
For a quick workaround, one can use this code in onCreate method of activity with launchmode as singleTask.
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
return;
}
This might help to avoid unnessary execution of code written in onCreate. However this is a recommended solution as it doesn't solve rootcause of problem.
I have an html page with two links as follow:
Test 1
Test 2
I open it with android Internet app. In my "Hello word" manifest application i declare:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter android:label="testapp">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myApp" />
</intent-filter>
</activity>
I can open my application with both links.The first time i click on both links my app starts and passes through onCreate (my app was not in background before click). The second time (app now is in recent tasks), the first link clicked before in chronologically order pass through onResume only but the second pass on onCreate. This will be the behaviour if i try open links n times until i kill application from recent. Any ideas?Thanks!
An Android activity, by default can be instantiated multiple times. You should use android:launchMode="singleTask" to override this behavior. There are some other flags that might interest you if you - here is the link to documentation.
I have added a connection to my app for an existing contact and my app's icon shows up for that Contact in the native Contacts App. However, when I touch my app icon, Contacts App crashes. Please let me know which part of code I should paste to help you guide me. Appreciate your help.
hey add this code into AndroidMainfest.xml
suppose you want open first activity on icon click.then add this intent filter into that activity
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT">
<data android:mimeType="vnd.android.cursor.item/vnd.package_name.activity_which_you_want_to_open" />
</intent-filter>
Have you added intent filter to your app like one below, If not add it to your main activity in Manifest.
<activity android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
I've searched documentation, but I'm missing something obvious. Or am trying to do something backwards.
What I have is the main activity, that's fired from the launcher and widgets. I have a second activity that I'd like to be fired off when a URL of a specific pattern is attempted to be opened. Here's the two activity definitions:
<activity android:name=".activities.MainMapScreen" 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=".activities.ViewDatasheet" android:configChanges="orientation|keyboardHidden" android:label="#string/app_name" android:process=":BMMapsDatasheet">
<meta-data android:name="Main Screen" android:value=".activities.MainMapScreen" />
<intent-filter>
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.ngs.noaa.gov" android:pathPattern="\\/cgi-bin\\/ds_mark.prl\\?PidBox\\=([a-zA-Z]{2}[0-9]{4})" />
</intent-filter>
</activity>
Like the above, url's aren't intercepted. Even if I have nothing in the data but the scheme and host, I don't get the prompt to select my app.
As a test, I copied the browsable and to the main activity, and when I do that, the icon disappears form the launcher altogether. (And I still get no prompt when trying to hit the URL.
All the examples I find has the in the main activity, not a secondary activity, so.. I'm not sure if I'm missing some flags or such.
Any thoughts would be appreciated. If you want me to post the whole Manifest, let me know.
--Mike.
Edit: After adding #iturki suggestions, there was no change. however - if I also added <category android:name="android.intent.category.OPENABLE" /> to the action to handle the URLs, that worked. None of the examples I saw with the had that category, so I'm not yet sure if it would affect other operations of the activities in my application or not.
Try adding a VIEW action to your second Activity's <intent-filter>:
<action android:name="android.intent.action.VIEW"></action>
And I think you might want to add your Intent to the DEFAULT category too:
<category android:name="android.intent.category.DEFAULT"></category>
I'm not sure if it is important but all the <intent-filter>s have this category.
I've an application with a lot of activities. When returning from desktop or lockscreen, the expected behaviour is, that the last used activity is shown again.
This works flawlessly if the application is started again from the normal desktop icon. But if I add it to the lockscreen "widgetlocker" and start it from there, there is always the main activity shown. And unfortunately, if I close the main activity later by returning via back button, I return to all other instances of this main activity.
It might be related to the fact that I'm returned to the lockscreen when closing the application directly after having opened it from the lockscreen.
But I'm quite puzzled why the behaviour is different if I open from lockscreen or open normally.
Any suggestions? How can I force the application to be ALWAYS return to the last used screen?
...
# main activity:
<activity android:name="StudyOptions"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|locale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="com.ankidroid.category.DECK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd.anki" />
</intent-filter>
</activity>
<activity android:name="DeckPicker"
android:label="DeckPicker"
android:configChanges="keyboardHidden|orientation|locale"/>
...
how about maintaining the activity changes like a state machine. Giving a state number for each activity and persist the current in the database. And recover the last state from the database on startup and display the corresponding activity.