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.
Related
We use intents to switch between two activities and also fragments are for the same purpose. So why can't we use intents always instead of fragments?
Intent, you can think of intention to do some work. It can be either go from one activity to another activity, send email, open some links and so on.
Fragment is just like part of those activities.
To make it simple you can think of activities as full website page whereas fragment as a part of that website page. So, activitycan contain any number of fragments.
I think :
1.In the changing fragment you just change part of activity and not whole of them , but by intent you change whole of activity to other.
2.by intent you can communicate between android components such as activities, content providers, broadcast receivers and services, but by fragment you cant and in the otherwise fragment is child of activity.
I have a MainActivity with a container FrameLayout in which I change multiple Fragments (Fragment A, Fragment B etc).
In one of this fragments let's say Fragment A I have to open another activity (Activity X).
The problem is that from this activity when I press a button I have to change Fragment A with Fragment B (in the background somehow) and after that, slideout Activity X (with translate animation), and slidein Fragment B ,all this without restarting the MainActivity because I have to keep the state.
How can I do this?
Thanks
Android uses loosely coupled components as its main building blocks. As you know, Activities are one of the main Android building blocks. Thus, interacting between activities are very restricted to a few ways.
Passing data via Intents by startActivity(), startActivityForResult() etc. This way is useful whenever you are starting new activities.
Sending broadcast Intents. This could be useful once you want to send a signal to your another app's component.
Utilizing shared Application object.
Java static fields and some other ways.
In your case I would recommend you to use a Dialog Fragment instead of your second activity, if your second activity is just a login activity or something like that.
UPDATE #1:
If you really would like to keep your second activity, so I would personally recommend using local broadcast mechanism.
Also there are another way to get this done. You could start your second activity as startActivityForResult and then whenever user gets back from your second activity to your first one, your first activity can get informed by its onActivityResult method. There you could switch those fragments.
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.
I need some help :)
I have a tab activity with four sub activities. First and most important is a list activity, second also list, third is settings activity and fourth with help.
When app is launched tab activity creates specs with intents for sub activities and sets first activity as a current tab so that activity acts upon intent from spec and that works fine. Later a broadcast receiver can broadcast notification with intent to refresh first list activity because new data has arrived.
My goal is: When the user acts upon notification: If app is launched, so there is a task on stack but not on top, to refresh that existing list activity inside tab and bring the whole app to front. If app is already visible but on different sub activity (different tab) just to navigate to first tab and refresh first list. If first list (first tab) is already visible just to update it.
I can't seem to make this work. I've tried different strategies. Intent that targets tab activity. Intent that target sub activity. Combinations of launchMode and intent flags but i cannot make this work. It seems to me as a reasonable scenario.
Any insight will be appreciated.
OK this workaround worked for me:
TabActivity has an onNewIntent(...) method which handles intents that I want to pass to child activity. TabActivity will call a method on custom application object. That object will pass message to ChildActivity which passed it self as a listener on app object on creation.
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.