Intent filter detection in several Activities - android

I am developing an NFC application (although I think this doesn't really matter for my problem) where I currently have a MainActivity with a TabHost and some other activities (one per tab). I also have an Activity that I have done to read NFC tags. This activity contains intent filters in the manifest to catch the tags.
Right now if I scan a tag the last activity runs and launches a dialog. The problem is that it is an independent activity and the background is empty. I would like to launch the dialog in the current activity instead of launching a new one.
How can I do this? My first thought has been to extend the NFCReader activity in the other Activities but it doesn't work. I think the problem is that in the manifest I have specified the intent-filter only for the NFCReader activity, not for the rest, but I'm not sure.
So the question is: how can I use an intent-filter for all the activities of the application and launch a dialog in front of the current activity without starting a new one?

Move the NFC handling to the activity that has the TabHost, in your case based on description I think it is MainActivity. I got NFC to work correctly on TabHost by that way in my case.

Related

Check For Activity is onScreen when Notification is Clicked ?(using Intent)

I am trying to add upload image feature in my application, using android-upload-service.
I am facing a issue not specific to the library.
When i am clicking on upload notification I want to open a activity using Intent.
code:
val intent = Intent(context, EditorActivity::class.java)
intent.putExtra("ResourceId", resourceId)//No I18N
context.startActivity(intent)
But,
First requirement is to start activity if it's not onScreen.(i.e application might be dead, or i might be on different activity)
Second if the activity is already running then do nothing
How can i check is the specific activity is dead or running and on Screen.
current code is starting the activity on top of present activity because of what i am having same activity on top of each other.
Add launchMode="singleTop" to the <activity> definition in the manifest. That should be enough to do what you want. It tells Android that it should use an existing instance of the Activity instead of creating a new one, if an instance of that Activity already exists at the top of the activity stack in the task.
Please don't use one of the special launch modes singleTask or singleInstance as these usually create more problems than they solve if you do not know exactly what you are doing.
First, add android:launchMode="singleTask" for that activity in your AndroidManifest.xml file. This will make sure that there is only one instance of the activity at all time.
Example,
<activity
android:name=".activities.MyActivity"
android:launchMode="singleTask"/>

android activity start from other app causes issue when onResume

I have two activities, one is MainActivity (the launch activity), the other is MaskActivity. If I start my app from other app, and start MaskActivity from MainActivity (now MaskActivity is on top of MainActivity), problem arises when I press home button on the phone and re-enter my app, a new MainActivity is started and put on top of the MaskActivity and the old MainActivity, which is not what I want.
But if I start my app from program list(not from other apps), things act quite right as I wanted, when re-enter my app, no new MainActivity is started and MaskActivity is on top of MainActivity.
My MainActivity's launchMode is "singleTop", I don't know if this is the problem. Simply change it to "singleInstance" or others can not fix the problem. When changed to "SingleInstance", it won't start a new MainActivity but will bring MainActivity to top. But I want the MaskActivity stay on top when the program resumed if the MaskActivity is on top of the MainActivity before the program paused.
Any ideas?
What you're asking for is a little unusual. Usually apps that provide intent-filters have specific activities that can handle those intent filters. For example, if you have a image viewing application, the "image gallery" activity will handle the "view" intent-filter.
If I understand correctly, you're saying that both activities can handle the case where your app is launched from another activity? Perhaps your two activities are closely linked (in terms of functionality). Perhaps you should have just one Activity, and put the view content of the two Activities into Fragments. Those fragments can be hidden or shown within your Activity. Your Activity can be singleTask, and will always be showing the correct content when it is launched (as you want).

Could someone please explain FLAG_ACTIVITY_PREVIOUS_IS_TOP

I have an Android app with multiple activities. The main activity communicates over a network and can launch or dismiss various other activities depending on commands it receives over the network. When an Activity is dismissed I don't want to finish() it, just move it down the stack so it's no longer the top activity. What I really need is a FLAG_ACTIVITY_REORDER_TO_BOTTOM but there is no such thing.
There's an intent flag called FLAG_ACTIVITY_PREVIOUS_IS_TOP and the name implies something like that but I don't understand the description:
"If set and this intent is being used to launch a new activity from an
existing one, the current activity will not be counted as the top
activity for deciding whether the new intent should be delivered to
the top instead of starting a new one. The previous activity will be
used as the top, with the assumption being that the current activity
will finish itself immediately"
Could someone please decode that for me, and if it's not what I want IS there some way to tell an activity to submerge itself below the previous one?
This isn't possible. The activities are stacked and you cant put one back under the other. It sounds like you may want to create a class that extends Android’s android.app.Application.
I found this tutorial online and it looks good. Good luck.
Extending Android's android.app.Application tutorial
You cannot move an activity below a certain activity into the android back Stack. The only way to move a activity in back stack is to open another activity on top of it. You can move an activity on top by creating a single instance of activity using FLAG 'singleTop' in this way your activity will be moved to the top of another activity and only a single instance of activity will be there in stack.
More information about activity back stack and Flags is available here.
Go through this information and all your doubts will get cleared about back stack.

Android: Intent open the wrong activity

I'm working on a widget for an Android app and I encounter a problem. When the user click on the widget, I'd like the application to open his first activity (the one with intent-filter: 'action.MAIN' / 'category.LAUNCHER') (I'll call it Activity A).
To do that, I use this snippet (in my WidgetProvider class):
Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.view , pendingIntent);
This works very good if my application is killed or if all the activities have been closed (using the back button until the application close)
The problem happen when several activities are open. Imagine the activity stack is like : Activity A -> Activity B -> Activity C.
If I click on the widget, SOMETIMES the application is just brought to front and Activity C is visible instead of Activity A.
It's very strange because this doesn't happen every time. (It seems that it happen after I navigate a lot in activities)
I really need the activity A to open and not another activity whatever how the activity stack was because this activity is like a hub with several links to differents fonctionnalities.
Can somebody help me?
Thank you very much.
PS: Sorry if my English is pretty poor, I'm from Belgium
From a widget you should do more or less the same as the system launcher does with intents.
Try using FLAG_ACTIVITY_CLEAR_TOP in your intent. You will also want to look at FLAG_ACTIVITY_NEW_TASK.
It's better to follow the platform guidelines by the way, which say that you can also inject deep into your app, provided you give the user the right path to navigate up again.
If you want a specific Activity to launch, why don't you just call that activity specifically, instead of dynamically.
You are experiencing issues due to the Activities in your back-stack. Android manages the life-cycle of your Activities in a very special way that you should probably research - http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle
calling finish() - will always return to the activity that launched the current activity. The only time your app will close, is if there was not an original activity. You could always test for this condition, and if the calling activity is NULL, you could explicitly call you Activity A.

Accessing sibling tabs in an Android application?

I have an Android application using a TabHost to create several tabs with different activities in each tab.
When someone performs a certain action in the activity associated with tab A, I want to programatically switch to tab B, and call a function on tab B's activity to provide it with some data generated in tab A.
Is such a thing possible? I don't know how to access either the TabHost object or the B activity from within the A activity. I tried passing these objects in to the A activity by adding them as "extras" to the Intent, but this doesn't work, probably because the classes are not serializable. If I could access the TabActivity itself, that would probably be the best solution.
Thanks,
Frank
Implement a BroadcastReceiver in the TabActivity and another in Activity B.
Register both in the manifest with custom intent filter 'actions'
Get Activity A to send a broadcast with an Intent including extras that the TabActivity will receive.
Have the TabActivity switch tabs then send a broadcast with an Intent with extras that Activity B will receive/process.
To expand on this, think of it this way...
Under 'normal' circumstances an Activity is a stand-alone / self-contained entity. If it needs to have some action performed by another Activity, it shouldn't need to know anything about that other Activity, just a way to 'call' it, i.e., use an Intent set with the relevant action/category and extra data.
In the case of an Activity which is embedded as tab content, this mostly still applies. The Activity itself doesn't 'know' that it has been embedded as tab-content (nor should it). As such it knows nothing about the TabHost or Tabactivity (when using one). It also doesn't/shouldn't know specifically that there are other tabs with other Activities...
It is quite legitimate, however, for the TabActivity to know about the tab contents (it created them after all) and to be able to mediate between them. If you simply have Activity 'X' broadcast that a particular action has been performed and allow the TabActivity to receive and act on the Intent type/data then the various siblings don't need to know specifically how each other works.
That would be my approach anyway. :-)
You could stash the data in an object at the Application level. Then when activity B starts it could check for the data.

Categories

Resources