开发一个我有
的Android应用程序我有两个班级A班和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();
}
}
答案 0 :(得分:11)
您不应该创建活动类的实例。这是错误的。活动具有ui,生命周期和活动由startActivity(intent)
您可以使用startActivityForResult
,也可以使用意图将值从一个活动传递到另一个活动,并执行所需操作。但这取决于你打算在方法中做些什么。
答案 1 :(得分:8)
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
}
});
}
}
让我知道它是否有帮助...
答案 2 :(得分:3)
如果你需要从两个活动中调用相同的方法,为什么不使用第三个对象?
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();
}
}
当然使其静态取决于用例。
答案 3 :(得分:0)
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);
}
}
答案 4 :(得分:-1)
简单,使用静态。
在活动中,您具有要调用的方法:
private static String name = "Robert";
...
public static String getData() {
return name;
}
在您拨打电话的活动中:
private static String name;
...
name = SplashActivity.getData();
答案 5 :(得分:-2)
在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
}
}
使用此格式,您可以调用从一个活动到另一个活动的任何方法。