I'm trying to pass data from tab n to tab n+1 activities, based on TabActivity in the MainContainerActivity, using parcelables but can't succeed as I can not apply "getExtras" method on the tab n+1 intent.
I can pass my parcelable object from one tab to the next one if I start the tab n+1 activity using startActivity(nextactivity) instead of using the tab switcher with tab.setCurrentTab(tab_n+1).
How shall I manage the activity intents sothat I can succeed to pass those parcelable objects, still implementing TabActivity in the MainContainer class and Activity in the tabs ?
or is there a way to force the activity start in the manifest when using tab.setCurrentTab(tab_n+1) ?
I read dozen of threads and have been trying many misc tests but w/o success so pls detail the answer as for a real noob :-)
EDIT:
Looking to get this working from API8 onwards
As explained here :
Restarting an activity in a single tab in a TabActivity?
What you want is to destroy the activity and start it again, by passing the new Parcelable "arguments" in the intent.
The suggested method will probably work, however I strongly suggest you to consider using Fragments instead. If you want to support version prior Android 3, you will need the support package.
The examples listed in this thread: https://stackoverflow.com/questions/5710573/need-a-fragments-example should get you started on Fragments.
I guess the simple answer to my simple 3 tabs application is using a global object as described here, which also allow keeping my existing source code.
Related
For my current project, I will be using this SlidingUpPanel library.
The sliding up panel will host a fragment that will contain application/global level information. This panel will be in every activity in my application. This will be very similar to the way Google Play music works.
My question is, what is the best way to persist the sliding up panel fragment throughout my application? I see myself going about this in two ways...
Storing the fragment inside the Application class and loading it at the start of every activity (somehow, not even sure if this is a possibility).
Storing the data that it will display in the Application class & loading a new instance of the fragment, passing in the persisted data.
Which one of these two ways would be the best? Is 1. even possible? Are there any coding landmines with these approaches?
Storing a fragment to persist throughout the application lifecycle would be pretty unorthodox, mainly because the fragment class should be able to follow it's normal lifecycle events (in this case, mainly onPause and onResume) instead of being stuck somewhere in the application class.
It is definitely common practice to store the data and load it each time you display the fragment. If you want to enable some sort of cacheing or singleton pattern to access the data, it should most likely be with another object that the fragment can access but is not a member within the fragment.
There is a good video from google about leaking views and it touches briefly on the pitfalls of doing some similar to what you're proposing in bullet #1.
I think the structure of your app looks like it should be a single activity where that bar is in it, then the main content is a Fragment that you replace and use addToBackStack on in order to maintain the use of the back button. Otherwise, you are going to have a lot of repeated code with solution 2 (which means a lot of repeated work in the case of bugs etc., not very maintainable), or leak views using solution 1.
More info on providing a proper back implementation
I know many of you will direct me to the API. I am getting confused the more I read about Intent Fragment and Activity. Can anyone please describe what are these and why are these three important for the process of android application development?
Thanks for you help in advance.
1) Intent : -
It's an "intention" to do an action. It is like sending Message to Android OS to carry Out some task. For ex: Start other activity if some action happens. See Below Links :
http://developer.android.com/reference/android/content/Intent.html
2) Activity : -
It is a Single Screen that users Interacts with.It is the only component that can (and must) have a user interface. Learn Activity Life cycle. It is Very important. You Should declare your activity in manifest File.
3) Fragment : -
A Fragment is a behavior or a portion of user interface in an Activity. We can call it like sub Part of Activity.Just Remember that Fragment May or may not have view. It is Like Small Activity,but they can be multiple on single screens and we can interact with them. Read the Fragment Doc from Developers site. It is great place to Start. see this : - http://developer.android.com/guide/components/fragments.html
In Simple Words,
1) Activity: is a screen which hold view(s) for GUI components. A Window in Desktop Application. It has a lifecycle like created,paused, stopped like in window.
2) Fragment: is a component used for dynamic GUI development. it also has a own lifecycle. But only difference is that it can't be used directly. it should be encompassed in Activity in order work. An activity may have one or more than one fragments.
3) Intent: is a message passing framework from one activity to another. message can be anything causing an activity to resume, passing extra to an activity or cause an application to start.
I have been researching this for about an hour and cannot figure out whether to use fragments within an activity or start a new fragment activity.
Some sites make it sound as if you should have 1 activity and EVERYTHING else is a fragment. Is that the more proper way now? I can't figure out when you use an Activity (or fragment activity) and when you use a fragment.
I have an app for a conference with:
-Speakers (and sub views/activities/fragments) for each speaker.
-Schedule (different sections for each day)
-General info
-Sessions (different sections for each session).
So do I have 4 activities each with their own fragments or do I just use 1 activity with fragments and nested fragments?
You could do it either way, but generally it is best to use an Activity (or FragmentActivity) for each "screen".
If the user sees your app as logically a single screen that has little panels appearing/disappearing for different kinds of data, then use one activity with a lot of fragments. If the user sees it as "going to different screens", then you probably want multiple activities.
If you go with the one-activity-many-fragments model, you may find that your activity's code gets really complicated dealing with all the possible configurations of fragments. That is a good sign that you may want to split it into multiple Activities. Similarly, if you go with the many-activities model, but find that things get complicated as you pass shared data between activities, consider merging the activities.
Converting from Activity to FragmentActivity is as simple as changing the extends and nothing else needs changing.
My conclusions:
I stopped using Activity and only use FragmentActivity as it is more flexible and more up to date and backwards compatible (Using the support library).
If the FragmentActivity has a component that is large enough to be a standalone component, or needs to be one, then I make it as Fragment.
I haven't come across something that would require a complete separate activity to be within another activity, but that should only be used if that component is large enough and completely standalone enough to need an activity for itself.
I don't fully understand your app to be able to make a specific call on which you should use, if you want my opinion, can you provide more details on what you are working on and how are those components connected.
Regards
Another consideration in choosing a more decomposed architecture (many Activities) might be the cost of destruction / creation in the Activity Lifecycle. Do you plan to use Explicit/Implicit intents to leverage existing apps? More death. So, you might have only one activity in a dispatch oriented model and clearly see your apps logic in one place, but how much state will you have to save/restore? Are there performance penalties for re-inflating or populating data resources?
I am new to Android development. After learning from many tutorials I got many Activities and many Fragments. How can I make a core engine to check what Activity is running and what Fragment is showing on a container?
Assume that I have:
Acivity01, Activity02, ... , Activity10
Fragment01, Fragment02, ... , Fragment10
I want to make a class that filters the Activity where Activity is on runtime and what Fragment is embeded to that activity.
How can I do this?
If I understand you correctly, you may want to store some references within your Application class to an Activity and to Fragment instance(-s), which are currently in foreground (by this I mean that user can instantly interact with Activity/Fragment).
As for Activity
Create some Activity field in your Application class and getter/setter methods for it (e.g., setCurrentActivity(), getCurrentActivity()). Then call setCurrentActivity() from onResume() method for each of your Activity instances. Don't forget to call setCurrentActivity, supplying null reference to ir in order to properly handle a case, when there are no foreground activities, but application is stll working.
As for Fragment
The general idea is similar to the first item, but there can be more than one Fragment instance in foreground state at time. So you need to store something like List, where you add your resumed fragments and remove paused.
You may also want to implement something similar for dialogs, for example. Then use the same strategy. Hope it will help.
I am working with ActivityGroup and I want to open an sub activity of the ActivityGroup on selection of another tab. Please anyone let me know if it is possible or not?
example - I have tab1 and I open a subActivity(say activity "A") in the same tab using ActivityGroup and now when I click on tab2 I should open the same Activity "A" with all its contents.
Thanks in Advance.
I really, strongly urge you to stop using ActivityGroup. This has been deprecated.
The fragment support library provides a much better way to do these things. The 3.2 version includes an example showing how to use it with the tab widget. See the "Fragment Tabs" example and the TabActivity documentation.
These can be used down to Android 1.6.
(There are also lots of demos there for other things you can do with fragments.)
This is how I thing it can be done :
open the sub-activity just as normally you do, create certain static variable to hold data between two opened activities, be it static bundle or strings, when ever you perform some action on activity ex: type some thing in edit text, save the content in those static variables, in the onResume of the activity, put those data from variables in views........
See if it works........
Try this code,
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
if not found helpful then please provide me some more details about flow and if possible then put your (xml/java) code. so i can clearly understand the problem.
Let me know it's useful for you or not by comment ?