我想提示用戶使用到退出該應用程序的上回壓,但我很努力在我的片段來實現這個退出應用程序對話
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Really Exit?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
HomeFragment.super.onBackPressed();
}
}).create().show();
}
}
我不認爲你可以從一個片段覆蓋onBackPressed(),你需要從包含片段的活動中做到這一點 – has19
你可以在對話框中添加NegativeButton(Cancel),所以當用戶點擊取消按鈕,然後你可以解除對話。 –
你不能在片段 – darwin