Call a method before startActivity automatically

Viewed 336
2 Answers

Call this method() in onCreate() assuming you're not using any flag to startActivity

Explanation:

Whenever an activity starts, the activity lifecycle by order is

onCreate() -> onStart() -> onResume()

EDIT:

Use a helper class AppUtils.java

public class AppUtils {

    public static void startActivity(Activity activity, Intent intent) {
        method(); // your method, you can put it inside AppUtils
        activity.startActivity(intent);
    }
}

then simply call AppUtils.startActivity(intent) from anywhere in your project. If your method needs a context, pass the activity context to it.

  1. You can do it in onCreate() method which executes before onStart(). OR

  2. Use static method and run it before startActiviy().

Related