简体   繁体   English

如何从其他活动中调用主要活动中的方法?

[英]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. 在这种情况下,如果您想调用MainActivity,则MainActivity使用MainActivity UI做了一些操作,则您必须使用Intent( http://developer.android.com/reference/android/content/Intent.html ),因为MainActivity应该是首先恢复,然后才可以使用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. 在这种情况下,您可能需要向意图中添加一些其他附加功能,这些附加功能将由MainActivity代码(在onCreate或onResume中)进行解析,并调用适当的方法。

Make it static , pass in the activity , instantiate, or better yet rethink design approach? 使它static ,传递activity ,实例化还是更好地重新考虑设计方法? 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 ? 我认为您不应该在main activity中的另一个activity调用方法-创建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 如何从另一个活动中调用MainActivity中的方法

Sometimes you cannot make the method static because it depends on all sort of other state in your MainActivity. 有时您不能使该方法成为静态方法,因为它取决于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. 同样,想要在MainActivity上调用非静态方法也没有错-就像一个类在调用另一个类一样。

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: 创建自己的Application子类:

    public class MyApplication extends Application { MainActivity mainActivity; }

  2. Adjust the manifest: 调整清单:

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

  3. In MainApplication initialise MyApplication.mainActivity 在MainApplication中初始化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. OtherActivity检索MainActivity实例。

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实例调用方法:

mainActivity.someMethodOnMainActivtiy();

Declare myMethod() as static. 声明myMethod()为静态。

public static void myMethod()
{
...
}

Call it anywhere in your application by MainActivity.myMethod(); 通过MainActivity.myMethod();在应用程序中的任何位置调用它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*/

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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