Search code examples
androidandroid-layoutandroid-activitystatic-methods

How to stack common methods for different activities


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.


Solution

  • 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