I have 2 Activities:
1 master activity.
1 detail activity
I tried to provide a dataclass (serialized) to the child the common way with Intents.
But I figured out, when the dataclass becomes big (lots of text which can't be fetched from child) then it crashes.
I am looking for another design sharing the data and found only the way to use a Singleton for the hole app.
Is there a more elegant way to share a bigger dataclass between Activities?
Related
Ok so I'm creating an app. I have a "Creation" page, and a "Detail" page for the same item. The UI is the same, probably the logic will be the same as well, only difference i can think of now is that when creating, the Object backing my UI is local, and as an argument in the fragment I get the item "Name", when showing the detail, the argument is the item's "ID" and the data backing my UI is remote.
My question is, following clean architecture and all around good programming rules, is it better to separate this screen in 2 almost identical Fragments, separating concerns and allowing for future differentiations, also possibly avoiding a "god fragment" but having a lot of possible code duplication, or is it better to keep them in the same screen, avoiding code duplication?
From my experience, you should avoid code duplication as much as you can. I don't know how deep and complex is your app, but if you have two almost identical fragments, I would use a 'ParentFragment' that contains the common code, and extend your Fragments from it. This way you can customize both fragments in the future, while maintaining the common features.
I have an android app where I want to have different functionalities, like a search function, a history, user defined lists,... Each of these functionalities is accessible via NavigationDrawer, and I want to contain a ViewPager to allow navigation in Tabs in each of those specific functionalities. Finally, when I switch between functions via NavDrawer I want my app to restore the state the corresponding function was in the last time it was used.
I'm puzzled about the best way to implement this. Currently, I'm using one Activity, which contains the logic for functionality navigation, and saving and restoring of current state of each fragment. I'm using one ViewPagerAdapter to hold information about current active fragments.
Another possible approach would be to make an Activity for each top-level functionality, and then implement the Fragments as needed. A third approach I can think of would be still using only one Activity, but different ViewPagerAdapter to hold the state information of each top-level function.
I'm really not sure which is the best way to go here, and I would like to settle on one of those possible options before going further, but I can't really find any useful information on the net. Are there any Best Practices known for my given scenario, or is this really just an opinion based topic?
Edit: No duplicate of Difference between Activity and FragmentActivity because I'm not even tackling the issue of support library vs. native
Edit2: I think it should be also possible by using nested fragments, i.e. having a fragment which hold my ViewPager where I could add my fragments for that function. But I really would prefer not using nested fragments at all.
A Fragment is best thought of as "a group of reusable interface elements". So if you can think of reusable elements across the activities (or a single activity when it's recreated) then you use fragments.
Activity is best thought of as "a single screen with UI". So then..
I have some simple questions for Android Users.
If I use Single XML for Multiple Activity, What issues might come ??
How it ll help in Application PERFORMANCE, if we use this kind of approach ??
for example : If have same XML layouts A,B,C for A,B,C Activity, Instead of using multiple layout I want to use only single Layout i.e. A XML Layout for A,B,C Activity.
Note: I have tried this, works fine. But I want to know that any issues or problem using this kind of approach. Gone through some sites but din't get cleared picture
Suggestions or help will be appreciated.
Many Thanks ..!!
There's no problem at all. The XML will get inflated as separate instances for each Activity.
Note that because they are separate instances, any data on those forms won't be shared between them, though; you would need to pass data between activities as extras on an intent or through a persistence mechanism (databases, shared prefs, etc)
In Android application, I have one request that aims to use one common Activity/screen in many screens or in overall of the application.
For example:
Screen A contains screen common C (to get better sense, it contains of list of items)
==> during the working on A, C might be modified to have add/modify/remove any items.
Now to to Screen B and in turn it contains screen C also.
==> request: C must be the same as one in A and the data is obviously the same.
One more request: it doesn't need to store the list items of C in the DB or in the preference since I thought the loading data phase can reduce the smoothly of the screen B or A at the first time.
How's your opinion about this thinking?
Anybody has some ideas about this situation? I'm really appreciate so much.
Thanks,
Tri.
Screens or Activities in Android are independent. You can't nest Activities. I could think of two solutions :
a) Save your data as an array List and pass it to B from A when B opens. Since it seems both contain a common layout as part of their own layout, include that layout using <include layout. in your xml for both A and B layouts.
b) If you plan to use fragments, make C a fragment and use it in two activites A and B ( could go for better design approaches though when using fragments)
This question already has answers here:
Dilemma: when to use Fragments vs Activities:
(17 answers)
Closed 4 years ago.
I often need the different parts of my applications to have their own special behavior and UI, and I don't know how fragments can help. In most cases, I think it is quicker to create 2 different activities (e.g., 1 for tablets and 1 for handsets), and to share the common behaviors and events in a third class.
So, keeping this in mind, why should I use fragments ?
Fragments are more of a UI benefit in my opinion. It's convenient for the user sometimes to see two different views of two different classes on the same screen. If, in your moment of creativity, you decide it would be nice to display your application with, say, a listView that takes up half the screen and a webView that takes up the other half - so that when you click on a list item in fragment A it passes an intent to the webView in fragment B, and suddenly you see what you just clicked without the app switching activities - then you could use a fragment. That's just an example I came up with off the top of my head.
Bottom line: Fragments are two or more activities on the screen at the same time.
The benefits I see when using fragments are:
Encapsulation of logic.
Better handle of the lifecycle of the fragment.
Reusable in other activities.
The drawbacks I see are:
More code(For example, instantiating a fragment manager, adding the fragment transaction, writing the callbacks of the fragment)
Communication between fragments and activities is harder. As #jonney said it, you would need to deal with a parcelable interface to serialize your objects you wish to pass.
So, when deciding to use a fragment, I would ask myself the following questions:
Is the lifecycle of the fragment different from the activity's lifecycle?
If the lifecycle is different, you get better handling of the lifecycle using a fragment. For example, if you want to destroy the fragment, but not the activity. Such is the case, when you have a pager adapter.
Is the fragment going to be used in several activities?
The user input events will be reusable if you use a fragment.
Is the amount of communication between the fragment and the activity small?
If you need to pass big objects to the fragment, you would need to deal with the code that serializes them. Also, if you need to communicate between fragment and activity, you would probably need to implement interfaces. This, in most cases, adds complexity to your codebase. It's not a difference maker, but a criteria to take into account.
Google advises you to ALWAYS use Fragments.
Why? It's simple:
In the simplest case, Fragments are used like containers of activities.
Why do you need this? Again, it's simple.
Android 4 (ICS) supports both Smartphones and Tablets. This means the SAME application will be running on a smartphone and a tablet and they are likely to be very different.
Tablets have big screens which will be empty or unused - unless you assign it properly.
That means- Putting two fragments on one activity like Contact List and Contact Info.
The smatphone will display contact List, and on a touch- display the contact's Info.
On a tablet, the user will still see the list and the info will be next to it.
2 fragments- on one screen....
Smart? yes... supposed to be back compatible down to Android 1.6......
#############################################################
O.K, Already Knew That? then - just try to understand the case solved:
A lot of things work that way- list & details, Menus and Sub-Menus, Info, Detailed Info and some more detailed info.
You want a way to keep it natural and smooth for a tablet which you expect to preform that way, but can't expect smartphone to display it all like the tablet did...
Get it?
for more Information, check out this.
I really think you just need to catch the concept....
Historically each screen in an Android app was implemented as a separate Activity. This creates a challenge in passing information between screens because the Android Intent mechanism does not allow passing a reference type (i.e. object) directly between Activities. Instead the object must be serialized or a globally accessible reference made available.
By making each screen a separate Fragment, this data passing headache is completely avoided. Fragments always exist within the context of a given Activity and can always access that Activity. By storing the information of interest within the Activity, the Fragment for each screen can simply access the object reference through the Activity.
https://softwareengineering.stackexchange.com/questions/244771/why-use-android-fragments
Fragment primary support more dynamic & Large UI Screen like Tablet.Because Tablet screen is much larger than normal Handset. There is more room to combine & Interchange UI Component.
Fragment allow such design without the need for such complex change in the View hierarchy.
By divide activity layout in fragment, we become able to modify activity's appearance at runtime