私は、共通のインターフェース要素を作成するために、私が働いている会社のために共通の再利用可能なクラスを作成しました。
このクラスは、コンストラクトと同じように単一のパラメーター (アプリケーション コンテキスト) を受け取ります。
メソッドの 1 つでContentClickableRowWithIcon
、クリック アクションとして使用するインテントを渡すことができます。
完全なメソッド宣言は次のとおりです。
public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, Intent i, final Boolean chooser)
最後の属性は onClickEvent で使用され、Chooser を呼び出すか、単にインテントに入るかを決定します。
public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, Intent i, final Boolean chooser) {
LinearLayout ll = new LinearLayout(mContext);
// .. LinerLayout construction, has nothing to do with the action
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this is apparently getting ignored... (ps: i've tried i.setFlags as well)
final Intent intent = i;
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(chooser)
mContext.startActivity(Intent.createChooser(intent, "Complete With...")); // crashes here with: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
else
mContext.startActivity(intent); // this works fine
}
});
return ll;
}
コメントで述べたように、私がチューザーを使用する機能を提供していないときはいつでも、すべて正常に動作します (このリストのすべてが新しいアクティビティ フラグを取得します。私はこれをよく認識しており、この問題が解決されたときにクリーンアップします)
私がした瞬間、例外がスローされます:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
アイデアが尽きた…
/// EDIT:: 注目に値するのは、デバッグ時に、onClick アクションでインテントを使用する時間に達すると、インテントの flags 属性が268435456
addFlags と setFlags で設定されることです。268435456