23

我正在开发一个 Android 应用程序

我有两个班级 class A 和 Class B 。

在 A 类中,我尝试了如下代码片段,

如何从 Activity 调用另一个 Activity 中的方法。

public class FirstActivity extends Activity {  

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    }      

    public void method() {
        // some code
    }  
}

public class SecondActivity extends Activity {  

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.main2);

        FirstActivity fact = new FIrstActivity();

        fact.method();
    }
}
4

6 回答 6

17

startActivityForResult 模式更适合您想要实现的目标:http: //developer.android.com/reference/android/app/Activity.html#StartingActivities

试试下面的代码

public class MainActivity extends Activity {  

    Button button1;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        textView1=(TextView)findViewById(R.id.textView1);  
        button1=(Button)findViewById(R.id.button1);  
        button1.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View arg0) {  
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);  
                startActivityForResult(intent, 2);// Activity is started with requestCode 2  
            }  
        });  
    }  
 // Call Back method  to get the Message form other Activity  
    @Override  
       protected void onActivityResult(int requestCode, int resultCode, Intent data)  
       {  
                 super.onActivityResult(requestCode, resultCode, data);  
                  // check if the request code is same as what is passed  here it is 2  
                   if(requestCode==2)  
                         {  
                          //do the things u wanted 
                         }  
     }  

} 

SecondActivity.class

public class SecondActivity extends Activity {  

    Button button1;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_second);  

            button1=(Button)findViewById(R.id.button1);  
            button1.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View arg0) {  
                    String message="hello ";  
                    Intent intent=new Intent();  
                    intent.putExtra("MESSAGE",message);  
                    setResult(2,intent);  
                    finish();//finishing activity  
                }  
            });  
    }  

}  

让我知道它是否有帮助...

于 2016-09-19T17:25:17.023 回答
13

您不应创建活动类的实例。这是错误的。活动有 ui 和生命周期,活动由startActivity(intent)

您可以使用startActivityForResult或者您可以使用意图将值从一个活动传递到另一个活动并执行所需的操作。但这取决于您打算在该方法中做什么。

于 2013-10-29T18:41:58.677 回答
6

如果您需要从两个活动中调用相同的方法,为什么不使用第三个对象呢?

public class FirstActivity extends Activity 
{  

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

    }      

    // Utility.method() used somewhere in FirstActivity
}

public class Utility {

    public static void method()
    {

    }  

}

public class SecondActivity extends Activity 
{  

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        Utility.method();

    }
}

当然,使其成为静态取决于用例。

于 2013-10-29T19:00:28.193 回答
-1

很简单,使用静态。

在活动中,您有要调用的方法:

private static String name = "Robert";

...

public static String getData() {
    return name;
}

在您拨打电话的活动中:

private static String name;

...

name = SplashActivity.getData();
于 2018-09-26T12:40:10.443 回答
-1
public class ActivityB extends AppCompatActivity {

static Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);

    try {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String texto = bundle.getString("message");
            if (texto != null) {
              //code....
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void launch(String message) {
        Intent intent = new Intent(mContext, ActivityB.class);
        intent.putExtra("message", message);
        mContext.startActivity(intent);
    }
}

在 ActivityA 或服务中。

public class Service extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {    

        String text = "Value to send";
        ActivityB.launch(text);
        Log.d(TAG, text);
    }
}
于 2018-03-12T22:00:58.740 回答
-3

在 FirstActivity 中声明一个 SecondActivity 变量

像这样

public class FirstActivity extends Activity {  

SecondActivity secactivity;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
  }      

  public void method() {
    // some code

  secactivity.call_method();// 'Method' is Name of the any one method in SecondActivity

  }  
}  

使用这种格式,您可以从一个活动调用任何方法到另一个活动。

于 2016-09-19T17:37:45.370 回答