android - how to create a reusable function?

Viewed 26111

In my android project, I have many activities and some of them already extend other stuff like map activity or BroadcastReceiver.

How do I create a function that I can call from any activity, because I don't want to have to repeat any code in multiple activities.

thanks.

5 Answers
  1. Create a new Java class BaseActivity with abstract Modifiers and extends it with AppCompatActivity.

  2. Move all your methods under Java class BaseActivity.

package com.example.madbox;

public abstract class BaseActivity extends AppCompatActivity {

    protected void YourClass() {

    }
}

  1. Extends your Activities with BaseActivity but not AppCompatActivity.
Related