0

I'm developing an android application. I wanna start an Activity to immediately run a method like "public void newNote()".

something like this:

Intent i = new Intent(MainActivity.this, WriteActivity.class);
startActivity(i);

, but including the method i wanna run ("public void newNote()").

Note that the method returns nothing..

  • 1
    Possible duplicate of [Execute a method after an activity is visible to user](https://stackoverflow.com/questions/19312109/execute-a-method-after-an-activity-is-visible-to-user) – Krystian G Aug 09 '19 at 20:32

1 Answers1

0

With an Intent with a custom action:

Intent intent = new Intent(this, YourActivity.class);
intent.setAction("runMethod");
startActivity(intent);

In the starting activity, handle the incoming intent and filter for the expected action:

@Override
protected void onNewIntent(Intent intent) {
   super.onNewIntent(intent);
   if("runMethod".equals(intent.getAction()) {
      mymethod();
   }
}

Or within onCreate with getIntent();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    if (intent != null ) {
        if("runMethod".equals(intent.getAction()) {
            mymethod();
        }
    }
}
JakeB
  • 1,650
  • 3
  • 9
  • 15