[英]how to call a function in an activity from main activity
public class Activity1 extends Activity { 公共类Activity1扩展了Activity {
billreminder br;//billreminder is a function in activity2
public void onCreate(Bundle savedInstanceState);
setContentView(R.layout.main);
br.read(c);//c is a string
}
how do we call read function. 我们如何调用读取功能。
Create a Class and Extend Application
, move your read method to Application class. 创建一个Class and Extend
Application
,将您的read方法移到Application类。
Form your Activity call getApplicationContext()
to get the Application object to call read method. 形成您的Activity调用
getApplicationContext()
以获取Application对象以调用read方法。
Example: 例:
ApplicationCalssName bmodel = (ApplicationCalssName) getApplicationContext();
bmodel.read(c);
Make a function static
so that you can use it from any activity. 将函数设为
static
以便可以在任何活动中使用它。
If your method uses a context then pass the context as a parameter. 如果您的方法使用上下文,则将上下文作为参数传递。
But if you want to use the elements of second activity in which you have write a method then you should create a Application class and you have to move this method to application class. 但是,如果要使用在其中编写方法的第二个活动的元素,则应创建一个Application类,并且必须将此方法移至Application类。
There are a couple of ways you can do this. 您可以通过两种方法来执行此操作。 You can make the function in activity2 a static function.
您可以将activity2中的函数设为静态函数。 You can then call it like so:
Activity2.read(c)
然后,您可以像这样调用它:
Activity2.read(c)
Another way, is to pass an intent from Activity1 to Activity2 as follows: 另一种方法是将意图从Activity1传递给Activity2,如下所示:
in activity1: 在活动1中:
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("c", c);
//this will put a callback in the onActivityResult method for the Activity1 class
startActivityForResult(intent, requestCode);//requestCode is an int
in activity2 inside its onCreate method: 在activity2的onCreate方法内:
Bundle extras = getIntent().getExtras();
if(extras != null) {
c = extras.getString("c");
if(c != null && !"".equals(c)) {
read(c);
}
setResult(resultCode);//resultCode is an int
}
in activity1 again1: 再次在activity1中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//detect resultCode or requestCode, and do whatever you want
....
}
Avoid subclassing the Application class . 避免子类化Application类 。 From the android docs :
从android docs :
There is normally no need to subclass Application.
通常不需要子类化Application。 In most situation, static singletons can provide the same functionality in a more modular way.
在大多数情况下,静态单例可以以更模块化的方式提供相同的功能。 If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.
如果您的单身人士需要全局上下文(例如,注册广播接收者),则可以在首次构造单身人士时为该函数提供一个上下文,该上下文在内部使用Context.getApplicationContext()。
Also, I think you are confused. 另外,我觉得你很困惑。 You need to call a method on an object.
您需要在对象上调用方法。
billreminder br;//billreminder is a function in activity2
billreminder br; // billreminder是activity2中的函数
is wrong. 是错的。 I think you meant that br is an object.
我认为您的意思是br是一个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.