简体   繁体   English

每当有新活动在应用程序中启动时,我是否可以通过任何方法调用该方法?

[英]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. 您可能还需要寻找onResumeonStart方法。

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.

相关问题 如何在每次查看活动时调用方法? - How to call a method every time an activity is being viewed? 如何在另一个活动中调用此方法? - How can I call this method in another activity? 当元素出现 Selenium+Java 时,如何在每次测试中每次调用方法? - How can I call method every time in every test when element appears Selenium+Java? 应用启动时调用onCreateOptionsMenu方法 - Call onCreateOptionsMenu method when app starts 在Spring Boot应用程序启动后调用方法 - Call a method after Spring Boot app starts 每次启动或恢复时,Android应用程序/活动是否完全启动? - Android App/Activity To Start Fresh Completely every time it starts or resumes? 每次调用方法写入 Excel 文件时,如何添加一行? - How can I add a row every time I call a method to write into an Excel file? 每次显示活动时运行一种方法 - Run a method every time an activity is shown 每次调用方法时,如何增加数组[a] [b]的大小? - How to increase size of array [a][b] every time I call a method? 是否有任何方法可以调用 SecondActivity 在具有 Recyclerview 的 FirstActivity.Fragment 上的 Arraylist 上添加新项目? - Is there any method that i can call on SecondActivity to add a new item on Arraylist on FirstActivity.Fragment that has Recyclerview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM