質問

Androidでアクティビティメソッド呼び出しを傍受する方法はありますか(「On -Start。OnCreate」のようなStandArtの呼び出しだけ)?アプリ内のすべてのアクティビティに存在する必要がある多くの機能があり、(さまざまな種類のアクティビティ(リスト、設定)を使用しているため)唯一の方法は、すべてのアクティビティクラスのカスタム拡張機能を作成することです。吸う:(

PS私はRoboguiceを使用していますが、Dalvikは実行時にコード生成をサポートしていないため、あまり役に立たないと思います。

pssaspedjの使用について考えましたが、多くの合併症が必要なので、あまりにも手間がかかりすぎます(Antのbuild.xmlとそのすべてのジャンク)

役に立ちましたか?

解決

すべての反復作業を、他のアクティビティに組み込まれる別のクラスに委任することができます。このようにして、このオブジェクトの作成と、OnCreateのOndestroyメソッドを呼び出すことに繰り返しの作業を制限します。

class MyActivityDelegate {
    MyActivityDelegate(Activity a) {}

    public void onCreate(Bundle savedInstanceState) {}
    public void onDestroy() {}
}

class MyActivity extends ListActivity {
    MyActivityDelegate commonStuff;

    public MyActivity() {
        commonStuff = MyActivityDelegate(this);
    }

    public onCreate(Bundle savedInstanceState) {
        commonStuff.onCreate(savedInstanceState);
        // ...
    }
}

これは手間を最小化し、すべての一般的な方法と活動のメンバーを考慮します。それを行うもう1つの方法は、すべてのAPIのxxxactivtyクラスをサブクレスすることです:(

他のヒント

Roboguice 1.1.1リリースには、コンテキストに注入されたコンポーネントの基本的なイベントサポートが含まれています。見る http://code.google.com/p/roboguice/wiki/events 詳細については。

例えば:

@ContextScoped
public class MyObserver {
  void handleOnCreate(@Observes OnCreatedEvent e) {
    Log.i("MyTag", "onCreated");
  }
}

public class MyActivity extends RoboActivity {
  @Inject MyObserver observer;  // injecting the component here will cause auto-wiring of the handleOnCreate method in the component.

  protected void onCreate(Bundle state) {
    super.onCreate(state); /* observer.handleOnCreate() will be invoked here */
  }
}

を見てみましょう http://code.google.com/p/android-method-interceptor/, 、Javaプロキシを使用します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top