Upgrade/Migrate from Multiple Activity Intents to Single ViewPager & Fragments - android

I used to have 3 activities with 3 layouts.
1. HomeActivity.java (activity_home.xml)
2. HelpActivity.java (activity_help.xml)
3. SettingsActivity.java (activity_settings.xml)
Whenever i had to open other activities, i used animated slide-in-out intents using overridePendingIntent. In this way, all the variables of HomeActivity stayed in itself and HelpActivity's variables/methods were in itself and same for SettingsActivity. I used onCreate() in these to perform some activity specific code. and, android:onClick="fetchSarcasm" from activity_home.xml was calling specified method in HomeActivity.java. So, there were no conflicts. All the normal things, that happen in an AndroidProject with 3 activities and used intents to switch to one another, were happening as i wanted.
But now, I wanted to migrate to ViewPager instead of slide-in-out intent transitions. In this way, there will be only one Activity with an XML of ViewPager. This ViewPager and its FragmentPagerAdapter gets the Pages from 3 classes extending Fragment with relative XML layouts.
I want to ask: Where do i write my Page Specific Code for onCreate(), layoutView's onClick="" and for onChangeListeners. Because, 3 classes extending Fragment do not have any Context or findViewById() and they do not respond to android:onClick="method" in different layouts.
Do i need to mix all that code of three activities into one containing ViewPager? If so, this makes ViewPager activity pretty heavy. and, writing if(page=2){ dothis(); } seems absurd for all pages at every code point. Any checkbox(view) that is in page 3, becomes null in other pages.
I know this might be a stupid question. I have spent almost 4 days trying to achieve something but i can't. Also, this is just my 2nd month in android, so i am new. I do not use Action Bars, Tabbed Bars or Navigation Drawers.
I am available on SO Chat too if you want to ask something more.
I just want to know how do i merge all the work i did when I was not using ViewPager.

If you're only reading the Preferences once for each Fragment, you can move your previous Activity.onCreate() logic to each Fragments onViewCreated() or onCreateView(). Read your shared Preferences there and set your Checkboxes accordingly.
Fragments inside a ViewPager aren't necessarily recreated on each page change. You can change how many Fragments are instantiated at a time with ViewPager.setOffscreenPageLimit(int).
Fragment Lifecycles explained

Related

How to model cross fragment transaction (ongoing process) in Android MVVM application?

I am developing an app that has some ongoing process that almost all screens depend on.
Let's call it ActiveTransaction. So, one fragment prepares items, the other fragment prepares payment, another adds discounts, etc... If one part fails, everything needs to rollback.
So, basically, I need to achieve atomicity across multiple fragments. I've tried to use shared ViewModel for this, but since each fragment adds its own stuff, that shared ViewModel has become too big.
So, I wonder what would be the best way to handle those processes that stretch over multiple fragments.
Should I have perhaps a singleton object ActiveTransaction that is injected in each viewModel?
Is this maybe what other technologies call the app State?
I haven't found anything in the MVVM architecture guidelines.
You can create Tabbed Activity - blank Activity with tabs. New project -> Tabbed Activity.
You will have one activity and multiple fragments.
So all functionality will be in MainActivity. Your fragments will contain views, which will call methods from MainActivity. All objects and calculations will be in main activity, so if it fails (because of memory lack), everything will collapse too.
For Example:
You have one Tabbed activity MainActivity and 3 fragments: Item Fragment, PaymentFragment and DiscountFragment. MainActivity contains multiple methods, let's say prepareItems(), preparePayment(), addDiscount(). User can go through Tabs, opening fragments. MainActivity methods can be called only via fragments. So if MainActivity collapses, the whole process will collapse.
So you will achieve atomicity and user can see changes by going through tabs. You can replace Tabs Activity by other scenario with single activity and multiple fragments which don't have functionality but contain views which will call methods from MainActivity

Using android activities and fragments the right way

I need some best practices ideas for my app. I think there is too much boilerplate code right now and I don't know if I'm using activities and fragments the right way.
There are 3 activities (A,B,C). The app starts at A. I can navigate to B which is a simple list and each of the items are clickable to show the item more detailed in activity C. A should be the "root" activity, so I set the parent activities in the manifest file.
Now I want to have a DrawerLayout navigation on B and C, which actually represents A in a smaller way.
I created an abstract NavigationActivity class for taking the view of the activity and set is as the first child of the DrawerLayout and add the navigation as second child. This works very well, but since the navigation has a state I embed the navigation as fragment, because I thought that the state is shared then, but it isn't and I don't know why I should use fragments at all.
Should I use one activity and load different fragments as main content? Then my app consists of 2 activities and maybe 50 fragments, when I finished it. I think that the way I try to implement it is not correct.
As an example: When you enter the PlayStore, there is the navigation on the left. You can browse the app and so on. Do you thing/know that it's the same activity with different main content or are there more activities?
Can you help me? Thanks :)
Single Activity applications are possible, but are not necessarily a best practice. IMHO, fragments have a very complicated lifecycle, so use Activituies when you don't HAVE to use fragments.
If your problem is to share the state of your drawer fragment through activities, you could pass it as an extra to each activity, or keep it in a static class/variable to retrieve it at every start.
Don't forget to also save this state during the destruction of one of your activity.

Implement Tabs with multiple fragments in each tab with seperate back stack

I want to use multiple fragments in each tab of Tab Host.
I am googling for last 5 days but nothing is working in my case. I got a good working solution
Seperate Back Stack and Sample Project Here .This maintain a separate Custom Back Stack for each tab having lots of fragments and store Fragment object in Custom Stack. But when ever I want to re-add any fragment that I already created and stored in its custom stack(as an object), all it's life cycle methods are called once again as are called first time. This is the problem. In this case all the views of fragment layout are recreated and it behaves like a new fragment.
I want to implement functionality like tabs having activity group(in which lots of activities are combined in a single tab using activity group) with the help of fragments.
Please help me in solving this issue........

Use View or Fragment in ViewPager

I have a question about whether to use View or Fragment with ViewPager.
Background:
I have an Activity A that contains a ListView. Each ListView item opens Activity B. Activity B shows different content depending on which ListView item is tapped in Activity A.
Activity B's content is shown inside a ListView.
Question:
Now, instead of going back and forth between Activity A and B to switch contents, I have a requirement to implement horizontal view swiping to switch contents all within Activity B.
One solution I found (tried it and it works) is to create many instances of Activity B's ListView and use it with ViewPager + PagerAdapter.
Another potential solution found on the doc (haven't tried it) is to bring that ListView into a Fragment, create many instances of the fragment and use it with ViewPager + FragmentPagerAdapter or FragmentStatePagerAdapter.
My question is, what's the benefit of using each approach? Should I go through all the trouble of bringing the ListView into Fragment or just simply use ListView with ViewPager?
Thanks
A Fragment is a useful approach, I think, when you want to tie some UI business logic to a particular View (or group of). As you know, that individual Fragment has its own lifecycle callbacks and so forth, just as an Activity would.
Rather than having a single Activity host many ListViews through a single PagerAdapter, it may be cleaner to use the Fragment approach because the Fragment only needs to deal with the logic behind driving a single ListView.
This is a very similar situation to one I've just been facing. I'm showing various vertically scrolling forms (consisting of lots of input fields) within a ViewPager. In my case I have gone for the Fragment approach because in my case, it's possible that the ViewPager will actually need to display a completely different kind of view on certain pages. For example, on the first few pages, user input forms might be displayed. But on the final page, a graph will be displayed. A whole separate set of logic is required to drive that graph. To drive those input forms and one graph from a single Activity would get a bit messy, and I would probably need to contain the business logic in several delegate classes or something. So for me, Fragments were the obvious choice in the end. I have my InputFormFragment and a GraphFragment, and they each contain only the applicable logic for the Views that they supply.
Another thing to consider is that in the near future you too may want to display a different kind of View in your ViewPager. Or, you might want to have another UI layout altogether, perhaps one that doesn't use the ViewPager but displays them all side-to-side (e.g. a layout used on a large tablet in landscape mode). With Fragments, things are just far more modular and you could factor the code to do this quicker. If on the other hand you achieved your objective by using a single Activity that contains a simple PagerAdapter and all the logic for the ListViews within, you might find it takes more work in the future to support new kinds of Views or special tablet layouts.
One thing I will say is having implemented Fragments in a ViewPager myself through FragmentPagerAdapter and FragmentStatePagerAdapter, things can get a bit awkward if you have any special requirements; managing Fragments can be tricky sometimes. For example, for my UI I needed to be able to programmatically add and remove the ViewPager containing the Fragments. I also needed to ensure that the adapter in use didn't destroy Fragments once they had been shown, because I needed to collect data from all Fragments simultaneously at a certain point. Furthermore, I had to extend and modify FragmentPagerAdatper to make sure that the Fragments go through their onDestroy() properly and are removed from the FragmentManager when the ViewPager was removed.
Fragments enable a very modular way of constructing UIs for various screen sizes and orientations, and are excellent in how they allow you to encapsulate business logic and lifecycles for individual UI elements. However if your scenario really is just as simple as several ListViews in a ViewPager and you know that you will never need the modularity, then the overhead of Fragments could be an overkill.

Switching from TabHost to Fragments with Viewpager: Where to put all my Code

Until now I've used TabHost for my App to create 3 Tabs. Each Tab is represented by an Activity in which I get the layout via setContentView(R.layout.something) from an XML file.
So 3 Tabs, 3 Activities and 3 XML files.
Now I've stumbled upon fragments, which are the new and better way to go, so here is my question.
Fragments handle the UI, so create 3 Fragments which are handled by FragmentPagerAdapter. Inside each Fragment I create the Content via XML files.
But where do I put all the code about which button does what, read from database or write to it etc. So far that's all been in each Activity that was loaded by the Tabhost.
Do I put all that code into the onCreate() etc. methods of each fragment or is there a better and cleaner way to do it?
The approach you propose is pretty much fine!
In terms of what to do where, I'd recommend that you do anything to create/alter the the UI of a page (fragment) in onCreateView() of each fragment, and any logic (reading databases etc) in onActivityCreated(). I'd recommend steering clear of onCreate() in a fragment, because this is called before it is associated with an Activity (preventing you doing thigs such as managed queries to contentproviders). Button click listeners could be defined either in onCreateView or onActivityCreated().
Anything more specific, let me know. Dont forget, standard Pagers don't include a row of tab titles/icons, but Google ViewPagerIndicator and you'll find a library which you can use to do this.

Categories

Resources