How to call method in main activity from other activity?

北城余情 提交于 2019-11-28 06:46:29

问题


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?
    }
}

回答1:


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.




回答2:


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();
    }
}



回答3:


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();




回答4:


Declare myMethod() as static.

public static void myMethod()
{
...
}

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




回答5:


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*/

}


来源:https://stackoverflow.com/questions/12202432/how-to-call-method-in-main-activity-from-other-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!