How to stack common methods for different activities - android

I am using new activities for ActivityButtons. So I have like 10 different activities, all of them using the same footer. In the footer, there are buttons and lots of click events. I just copy pasted all the listener events for Footer buttons to a few other activities to test it. It works but I need to find a way to keep them all together somewhere so that I will modify only once when I need to.
I tried keeping all common methods in a separate utility class by making them static, however it has a limited use. I am having issues with references and non-static methods.
In my research I have read about the ViewFlipper. Would it be better if I used a single MainActivity for everything and a ViewFlipper to switch between layouts. I have read that this might cause some resource issues, since the one and only activity will be active all the time.
Do you have any suggestions for this problem?
Thanks in advance.

There are a few things to consider.
Fist is that footers are a somewhat problematic design pattern on android. Because of devices with soft buttons right below the screen area it is very easy for users to accidentally click system buttons when they are aiming for footer, and vice versa. If you are dead set on using a footer I suggest that you leave ample margin between it and the bottom edge of the screen to help mitigate this.
You have a few options for how to handle the layout and click listeners. You could make the footer into its own Fragment And simply add it to each Activity that needs it. This is arguably the more difficult approach, but it would provide a good learning experience with Fragments if you are up for the challenge.
Another option is to but all of your footer click listeners inside their own activity and then extend that with all of your other activities. Something like this:
public class FooterActivity extends Activity{
Button btn1
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.yourLayout);
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(yourClickListener);
//...
}
}
Then in your normal activities change extends Activity to extends FooterActivity

You can create a ParentActivity, which will handle onClick method, and then let your other Activities to extend that ParentActivity to do what is specific for them.
The other approach would be to use Fragments. Create one fragment that will handle the Footer, and other would be different based on work they are expected to work.

Related

May two View subclasses work as two Fragment in one screen?

I have a little app wich contain MainActivity and two subclasses of View - FirstScreenView and SecondScreenView.
Now MainActivity manages both subclasses and output them successive.
I want to show both classes in one time in a one screen (in this layout - firstscreen.xml that is used instead of main.xml now. main.xml does not exist).
I know that this challenge can be overcome by applying the fragment. But my classes extends View, not Fragment.
May two View classes work as two Fragment in one screen?
Please, tell me in what way and how should I solve this problem.
Thank you (and thanks Google Translate).
Talking from a UI perspective, the app will look the same. Talking from a functional perspective, the app could do the same. The only difference is that if you use fragments, you will have to manage both lifecycles.
I rather preffer what you have done, working with these two views. Remember that if you work with custom views, it is a best practice to delegate its logic to the activity, something you will relay to the fragmeents in the case you use them.

Keep footer for multiple activities

I'm creating an app which requires the footer to remain constant among various activities. That is, when animating to another activity, the footer doesn't animate with it - it stays there. Here is an example of what I mean: http://www.youtube.com/watch?v=EwXjdTvVXHQ&feature=related
I know it's an awesome app, but please don't forget my question ;)
Use Fragments to switch out your content, then you can keep one activity with the same footer. Fragments are backported all the way to Donut.
Use only one activity with footer and body in your app, switches and animates body only just like switches in activities.
Create a dummy class that extends an Activity and add footer to it.using xml (or the sameway you would do for other activity).
And extend this class in your app wherever you want footer.

Android - How to change Activities within a TabActivity?

I currently have a TabActivity which has 4 tabs, within one of the tab's I want to be able to move forward and back between 4 different Activities.
However if I try to start a new Activity now it removes the TabActivty and starts a whole new Activity with no tab bars.
I have read about using view groups but that this is not best practice and also about using a view flipper but this doesn't seem to let me switch between different Activities only change the views within the Activity. I can't implement back functionality for exa,ple.
Can anyone point me in the right direction as to what I should be looking for as a solution to this?
EDIT:
Some more information:
Within the TabActivity my first screen will be a ListView that contains 4 rows, then selecting one of these will in turn load another ListView with 2 rows again within the TabActivity and then the 3rd screen will just contain some text depending on which option the user chose again within the Tab Activity.
Is a ViewFlipper the best solution here? It seems to me that it will require a lot of coding within one Activity if I use the ViewFlipper?
I have done something similar. I used the ViewFlipper to achieve this. You can override onBackPressed in your Activity so you can deal with moving back through your views.
There's a couple of ways of doing this but a simple way would be to just increment a counter in your Activity as you move to the next views, then in your onBackPressed method if counter != 0 just show the previous view, if counter == 0 call super.onBackPressed.
You can see in my video showing what the result could look like (ignore the bug being shown in the video).

ActivityGroup VS View flipper

I have a group of tabs. In one of the tabs, I want to display two different types of functionality.
So my question is it better to implement a activity group with child activities or just create a view flipper and switch between views.
ActivityGroup is used to manage one or more activity example. if two different activity have different implementation but the user want to see on sharing screen basis means in first half user want to see some thing and in second have some other thing. In that case ActivityGroup comes to play
ViewFlipper can be used if you want to periodically change the views. Say like an automated flipping book of some sort. Though a custom-adapter gallery is much better at this.
Both have different purpose. It depends on your requirement you will choose any of them.
i would go with a view flipper. you probably dont need the whole overhead of creating another activity. but that way will make your single activity that will hold them a bit larger. so also maybe try to implement some of the functionality inside the views you will be adding.
hope this helps.

Converting Multiple Activites into a Single Fragment

I've recently decided to update my app to support the new fragments feature in honeycomb 3.0.
My Application currently works on a list view that opens different activities depending on which list item is clicked.
Using an adaptation of the code in this tutorial I have created an app that consists of only two activities, but depending on which list item is clicked the second "viewer" activity launches using a different layout xml.
Unfortunately I haven't been able to figure out how to call the old methods that had all the functionality. Should I Import all of my old activities and then call the methods into the viewer activity (I may need some advice on how exactly to do this) or should I just put all the methods directly into the same viewer activity (please consider the size of these methods(which is very large by the way)).
Once everything is working with two activities upfront then it will be a pretty simple task of "fragmenting" the app as demonstrated here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
Thanks
As James has pointed out you will have to move the business logic from your Activities to your Fragments.
To handle events you can create a listener Interface. The CONTAINER activity/ies will implement this interface. As fragments has access to the container activity you will be able to delegate to the container Activity the "logic" for the desired events. For this events the activity will decide whether to launch a new activity, show/hide new fragments or whatever.
I had a similar question, take a look to the question and answer: here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
I think its possible to allow multiple fragments to occupy the same space in an activity. Again, take a look to the answer here ... I think the concept/scope of Activity has change a bit and now an Activity can contain different Fragments which every one will allow user to do a single focused thing.
I'm not sure what you mean by "call the old methods that had all the functionality". You'll want to rewrite all of your activity classes as fragments. Check out this tutorial here (it's very concise). Basically, you'll want an activity that consists of a ListFragment and a FrameLayout. Your ListFragment will update the FrameLayout by changing to the appropriate Fragment based on which row was selected.

Categories

Resources