[英]Is There any method by which I can call a method every time a new activity starts in an app?
I am using an external jar
for some functionality in my Android project but I need to call the initialize
function on every onStart()
function. 我在Android项目中使用外部
jar
来实现某些功能,但是我需要在每个onStart()
函数上调用initialize
函数。 Is there a method, I can skip this step and it will automatically gets called every time a new activity start. 有没有一种方法,我可以跳过此步骤,每当一个新的活动开始时它将自动被调用。
I am aware that some code injection techniques are available. 我知道一些代码注入技术可用。 Can some one please explain it in details or provide some source for it.
有人可以详细解释一下还是提供一些资料。
你可以试试这个:
protected void onCreate(Bundle savedInstanceState);
Make a class extend Activity class and put call to your library method in it's protected void onCreate(Bundle savedInstanceState);
创建一个扩展Activity类的类,并在
protected void onCreate(Bundle savedInstanceState);
对库方法进行调用protected void onCreate(Bundle savedInstanceState);
method. 方法。
Now make all your custom activities in which you need to call your library method on create extend above class rather than extending Activity directly. 现在,进行所有需要在create上调用库方法的自定义活动,而不是直接扩展Activity,而是在类之上进行扩展。
In short use Inheritance! 总之使用继承!
onCreate(Bundle bundle)
is the answer. 答案是
onCreate(Bundle bundle)
。 You may also need to look for the onResume
and onStart
methods. 您可能还需要寻找
onResume
和onStart
方法。
You might acheive this by creating a custom Activity class and overiding its onStart method. 您可以通过创建自定义Activity类并覆盖其onStart方法来实现。 Then extend all your Activities from this custom Activity.
然后从此自定义活动扩展所有活动。
public class CustomActivity extends Activity{
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
// call your method.
}
}
and then extend your activities from this custom Activity. 然后从此自定义活动扩展您的活动。
You do this as follows without modifying each activity. 您可以按照以下步骤进行操作,而无需修改每个活动。
Create a class which extends android.app.Application
like 创建一个扩展
android.app.Application
类的类
public class YourApplication extends Application {
@Override
public void onCreate() {
// do whatever start-up processes you want to perform in this method
}
}
Next, modify the application manifest files as follows 接下来,如下修改应用程序清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="application.package"
android:versionCode="1"
android:versionName="1.0" >
<application
...
android:name="application.package.YourApplication"
... >
</application>
</manifest>
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.