設定画面にボタンが欲しい。この正確な質問はここで尋ねられました。しかし、残念ながら答えはありません。:(
これに到達するために、次のようなカスタム設定を作成しました-
public class CustomPreference extends Preference {
private LinearLayout mWidgetContainer;
private View mRowView;
public CustomPreference(Context context) {
super(context);
}
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected View onCreateView(ViewGroup parent) {
LayoutInflater viewInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRowView = viewInflater.inflate(R.layout.preferences_row_view, parent, false);
mWidgetContainer = (LinearLayout) mRowView.findViewById(android.R.id.widget_frame);
Button button = new Button(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.listview_row_bg);
button.setTextSize(14);
button.setPadding(5, 5, 5, 5);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), BuyScreenActivity.class);
getContext().startActivity(intent);
}
});
button.setTypeface(null, Typeface.BOLD);
button.setText("Buy now");
mWidgetContainer.addView(button);
return mRowView;
}
}
これは機能します。しかし、それは奇妙な振る舞いをします。そのボタンをクリックするとわかるように、ユーザーを と呼ばれるアクティビティに連れて行きますBuyScreenActivity
。奇妙な部分は、もう一度押すと、BuyScreenActivity
設定画面に戻りますがonDestroy
、まったく呼び出されません。なぜそのように振る舞うのでしょうか?onStop
BuyScreenActivity
設定画面を下にスクロールすると、onStop
&onDestroy
が呼び出されます。なぜこれがそのように振る舞わなければならないのですか?