Android NFC - activity is not called - android

In my app I have 3 activities A, B, and C. For activity C I've declared intent filter TAG_DISCOVERED. But when I put tag to the phone, the current foreground activity's onNewIntent() method called. I want nfc tag to start my C activity, no matter what activity is in foreground. If my app is in the background nothing happens at all, no activity is called.
<activity android:name=".WelcomeActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ContactsActivity"/>
<activity android:name=".NfcActivity">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
</intent-filter>
</activity>

Related

What does it mean when two or more activities, each having intent-filter with action=android.intent.action.ACTION_MAIN?

Doc says https://developer.android.com/reference/android/content/Intent.html#ACTION_MAIN is an entry point.
Example code:
<activity android:name="org.A.A"
android:theme="#style/NoTitle"
android:screenOrientation="behind"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name="org.A.C"
android:theme="#style/NoTitle"
android:launchMode="singleTop"
android:screenOrientation="behind">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name="org.A.B"
android:theme="#style/NoTitle"
android:launchMode="singleTop"
android:screenOrientation="behind">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
1) So using android.intent.action.ACTION_MAIN acts as an entry point to the parent component (parent component I mean the activity or receiver or service)?
2) If yes, Entry point from where, as there is no CATEGORY mentioned.
Android apps are composed of different components. e.g. Activity, Service, BroadcastReceiver, and ContentProvider and each component can act as an entry point of the app.
Let's take activity as an example, you have defined an activity in your application with following action
<intent-filter>
<action android:name="com.yourapp.SOME_ACTION" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
and I start an activity in my app with following intent.
Intent intent = new Intent("com.yourapp.SOME_ACTION"); // same action
startActivity(intent);
Now what will happen? System will search for activities with com.yourapp.SOME_ACTION action and if it finds one (in current scenario, it will be activity you have created with com.yourapp.SOME_ACTION in your app), it will start your app (if it is not already started) and will open the activity in your app.
See, now I can enter into your app by using Activity with com.yourapp.SOME_ACTION. Same thing happens in case of other components.
Activity with MAIN action will be entry point of application.If you have one its well and good if more than one then you can have multiple activities through which you can enter into application.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
But if you have provided CATEGORY for activity it will create launcher for that entry point.
<category android:name="android.intent.category.LAUNCHER" />
Suppose two activities is having both MAIN action and CATEGORY as launcher two app icon will be created one will have one activity as entry point second will have another one as entry point.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Activities' stack (after exit the app, the system clears stack if launchMode="singleInstance")

I don't understant why is this happend: I need to create every Activity in single instance, so I put android:launchMode="singleInstance" in Manifest. But when I push 'Home' on the device and open it again the system clears the stack and opens the root Activity, not the last one that was open. How can I keep the last open Activity for the user but keep singleInstance in Manifest?
EDIT:
Manifest
...
<application
...>
<activity
android:name=".ui.login.LoginActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activity.MainActivity"
android:label="${app_title_constant}"
android:launchMode="singleInstance"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<data android:scheme="${scheme}"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
<activity
android:name=".ui.more.service.ScannerActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme"/>
<activity
android:name=".ui.activity.CardsListForSendActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activity.CountriesActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"/>
<activity
android:name=".ui.activity.StepperCardPurchaseActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait"/>
<service
android:name=".service.CacheContactsIntentService"
android:exported="false"/>
<service
android:name=".managers.push.PushNotificationExtenderService"
android:exported="false">
<intent-filter>
<action android:name="com.onesignal.NotificationExtender"/>
</intent-filter>
</service>
<receiver
android:name=".managers.push.PushBroadcastReceiver"
android:enabled="true">
</receiver>
</application>
</manifest>
Do not use launchMode="singleInstance". This is not necessary, and actually isn't working for you anyway. Normally, specifying "singleInstance" will cause each and every Activity to be launched into its own separate task. This, however, won't happen in your case because you have not specified "taskAffinity" in the manifest. Since all activities, by default, have the same task affinity, they will all end up in the same task (even though you specified "singleInstance" launch mode).
Remove all the launchMode specifiers and try again.
The standard behaviour of Android is to return the user to the task stack (exactly as he left it) when an App is brought from the oforeground to the background (after pressing the HOME button and returning to the app).

How do I select the activity to start?

I am working with android studio and combined three apps together. All the apps have been imported with their manifest file and each of those manifest files has an activity that is the main one that starts first. I have noticed that whichever application I import last, its main activity becomes the main activity of the whole project when its run. How do I change that?
Well it depends on the requirements of your application, you can define an Activity as "MainActivity" by defining the filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This is an example:
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
But you can choose other Activity as MainActivity , for example my application starts with SplashActivity to load all neccesary resources and then finish and start my "MainActivity".
<activity android:name=".activities.SplashActivity"
android:theme="#style/SplashTheme"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can keep other Activity as your MainActivity, probably an Activity that make transactions between Fragments, and you never finish it until you need to close completely the application.

Implicit Intent not being called

I am trying to use Implicit intent to launch an activity within the same application and for an activity of another application(my other application, not the native one), but couldn't succeed in any of the cases.
Here is my sample code for the first part (i.e. to launch an activity within the same application):
Inside Activity TESTActivity
Intent intent = new Intent();
intent.setAction("com.myapp.game.myimplicit_action");
startActivity(intent);
and here is my manifest file declaration for some activity say 'ImplicitActivity' with the same action:
<activity
android:name=".TESTActivity"
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=".ImplicitActivity">
<intent-filter>
<action android:name="com.myapp.test.myimplicit_action" />
</intent-filter>
</activity>
Both the activities TESTActivity and ImplicitActivity are in the same application under same package. Still my ImplicitActivity activity is not getting called.
I have figured out the problem. Posting the answer for the others facing the same problem.
We need to add Default Category in order to make Implicit intents work. So here is the correct manifest entry for the same activity :
<activity
android:name=".TESTActivity"
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=".ImplicitActivity">
<intent-filter>
<action android:name="com.myapp.test.myimplicit_action" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Too many Activities being started when launching an Android Application

I am confirming about creating activity.
My Manifest.xml is like this :
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity"
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=".SecondActivity"
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=".ThirdActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can see property action android:name= property is "android.intent.action.MAIN" and
category android:name= is "android.intent.category.LAUNCHER" for all activities.
When the application starts up, it calls FirstActivity.
Then calls useless Activity such as ThirdActivity or SecondActivity.
In this case, is my manifest.xml correct?
Or, do I need to set another property to Second and Third activity?
If so, what is that?
I wonder manifest.xml file is right for my case.
Please advise.
Try this config:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity" 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=".SecondActivity" android:label="#string/app_name">
<intent-filter>
</intent-filter>
</activity>
<activity android:name=".ThirdActivity" android:label="#string/app_name">
<intent-filter>
</intent-filter>
</activity>
Think of an Intent as message used to start an Activity to do something. So I can create an Intent to view a web page and an application with an Activity which knows how to view a web page - most likely the browser - can intercept his Intent as act on it.
You tell Android which Activities can act on which Intents using the <intent-filter> part of your Manifest.
The MAIN Intent is a special one. This is sent to an application when it is launched and basically it says "Go!" So the Activity which shoud be displayed first needs to intercept this by having a correctly defined <intent-filter>.
As you had all three Activities with MAIN in their filter they all responded to the request to start your application. So you should have that <intent-filter> only for FirstActivity.
One of the other problems with using
<category android:name="android.intent.category.LAUNCHER" /> for more than one activity is that the Phone's launcher menu will display more than one icon...
From the docs:
CATEGORY_LAUNCHER The activity can
be the initial activity of a task and
is listed in the top-level application
launcher.

Categories

Resources