简体   繁体   中英

How to call method in main activity from other activity?

I want to call public method in main activity from other activity. How can I do that?

class MainActivity extends Activity {
    public void myMethod() {}
}

class MyActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // How can I call myMethod() in MainActivity?
    }
}

It depends.

  1. In the case if you want just to use some shared functionality (as example the code which does some calculation).

    I would recommend to move this shared functionality to some standalone class and call it from there.

  2. In the case if you want to call MainActivity, so MainActivity did something with MainActivity UI, you will have to use Intent ( http://developer.android.com/reference/android/content/Intent.html ), because MainActivity should be resumed first and only after this it can do something with UI.

    In this case, you may need to add some additional extra's to intent, which will be parsed by MainActivity code (in onCreate or onResume) and call appropriate method.

Make it static , pass in the activity , instantiate, or better yet rethink design approach? I don't think you should be calling a method in another activity from your main activity - might be better to make a new class ?

Static Code:

class MainActivity extends Activity 
{
    public void myMethod() 
    {
        MyActivity.runMyMethod();
    }
}


class MyActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

    public static void runMyMethod() 
    {
        //Run code...
    }
}

Instantiate Activity:

class MainActivity extends Activity 
{
    public void myMethod() 
    {
        MyActivity myActivity = new MyActivity();
        myActivity.runMyMethod();
    }
}

class MyActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

    public void runMyMethod() 
    {
        //Run code...
    }
}

Pass Activity Reference:

class MainActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
            OtherActivity otherActivity = new OtherActivity(this);
    }

    public void yourMethod()
    {
    }

}

class OtherActivity extends Activity
{
    MainActivity mainRefrence;
    OtherActivity(MainActivity main)
    {
        mainRefrence = main;
    }

    public void onCreate()
    {
        mainRefrence.yourMethod();
    }
}

How to call method in MainActivity from another activity SOLVED

Sometimes you cannot make the method static because it depends on all sort of other state in your MainActivity. Making all the depend state also static is tantamount to just making everything global and this is just not a good idea.

Also there is nothing wrong in wanting to call a non-static method on the MainActivity - it's just like one class calling another.

Here's what you do:

Your Application is shared across all your Activities (provided they are all in the same process). This application can be used to store state. Although a sensible idea would be just to store the instances of your activities and let them store their respective states, which is what we're going to do.

  1. create your own Application subclass:

    public class MyApplication extends Application { MainActivity mainActivity; }

  2. Adjust the manifest:

    <application android:name=".MyApplication" ...

  3. In MainApplication initialise MyApplication.mainActivity

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyApplication ma = (MyApplication)getApplication(); ma.mainActivity = this; ...

  4. in OtherActivity retrieve the MainActivity instance.

private MainActivity mainActivity;

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

        MyApplication ma = (MyApplication)getApplication();
        mainActivity = ma.mainActivity;
        ...
  1. Make use of mainActivity instance to call method:

mainActivity.someMethodOnMainActivtiy();

Declare myMethod() as static.

public static void myMethod()
{
...
}

Call it anywhere in your application by MainActivity.myMethod();

If you to have static methods to call from any activity you should have a an Utililty or a Helper Class where you can call the methods staticly from anywhere I don't think that its a good pratice to bee caling static methods on one activyty to another

Here is an Example of an Helper Class

   public Class ActivityHelper{

    public static void myMethod(Context context){
    // If you need to do something with your Context

    }

/* and you can create a lot of  static methods that you would need to use from any activity or service on your app*/

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM