如何确认片段中的退出应用?当用户按回键

时间:2014-11-10 18:07:15

标签: android dialog fragment keyevent

我正在创建一个包含不同片段的应用程序。我想为后台键事件向侦听器添加代码,并与用户确认是否要退出应用程序..

public class FastivalesFragment extends Fragment {

public FastivalesFragment(){

    }
    CustomAdapter adapter;
    List<EventClass> tempRows = new ArrayList<EventClass>();
    EditText searchTxt;


    ListView list;
    View view;
     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
         Log.d("LifeCycle now is", "onCreateView");



        // searchTxt = (EditText)view.findViewById(R.id.inputSearch);


         tempRows.clear(); // to delete all objects form temp array

          view = inflater.inflate(R.layout.events_list, container, false);

          list = (ListView) view.findViewById(R.id.listView1);
          int j=0;
          while (AllEventsFragment.rowItems.size() > j){

              Log.d(" the value for region ", "is "+ AllEventsFragment.rowItems.get(j).getCategory());

              if (AllEventsFragment.rowItems.get(j).getCategory().equals("أخرى")) {
                  tempRows.add(AllEventsFragment.rowItems.get(j));
              }

              j++;
          }
              adapter = new CustomAdapter(getActivity(),R.layout.item_view,tempRows);
              list.setAdapter(adapter);



            //list.setTextFilterEnabled(true);
                list.setOnItemClickListener(new OnItemClickListener() {


                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        //Log.e("value of name ", " "+ arg2);

                        String name = tempRows.get(arg2).getName();
                        String details = tempRows.get(arg2).getDetails();

                        String ticketPrice = tempRows.get(arg2).getTicketPrice();
                        String boolingUrl = tempRows.get(arg2).getBookingUrl();



                      Log.e("sdate", " "+ sdate);


                        Intent i = new Intent (getActivity(), Event_details.class);
                        i.putExtra("name", name);
                        i.putExtra("details", details);
                        i.putExtra("sdate", sdate);
                        i.putExtra("edate", edate);
                        i.putExtra("time", time);

                        i.putExtra("boolingUrl", boolingUrl);




                        startActivity(i);




                        //Toast.makeText(MainActivity.this,"test",Toast.LENGTH_SHORT).show();
                        //Log.e("position"," "+ arg2);

                    }
                });




         return view;

        }




    //onCreate method
     @Override
     public void onCreate (Bundle savedInstanceState){
         super.onCreate(savedInstanceState);


         Log.d("LifeCycle now is", "onCreate");
     } // end of Attach

    //onPause() method
         @Override
         public void onPause(){
             super.onPause();
                //rowItems.clear(); // to delete all objects form temp array

             Log.d("LifeCycle now is", "onPause()");
         } // end of onPause
    //onResume() method
         @Override
         public void onResume(){
             super.onResume();

             Log.d("LifeCycle now is", "onResume()");
         } // end of onResume      



}

我使用以下代码进行活动,它工作正常..但它不适用于上面的片段文件。

    // confirm when user click back key
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode != KeyEvent.KEYCODE_BACK)  return super.onKeyDown(keyCode, event);

        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                //Yes button clicked
               finish();
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //No button clicked
                break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this)
    .setMessage("Are you sure?")
    .setPositiveButton("Yes", dialogClickListener)
     .setNegativeButton("No", dialogClickListener)
     .setCancelable(false)
     .setTitle("Exit");
     builder.show();

    return super.onKeyDown(keyCode, event);
}

当我复制这段代码上面的片段..我得到的错误:onKeyDown的方法对于Fragment类型是无法填充的。

1 个答案:

答案 0 :(得分:0)

我已经通过在主活动中添加上述代码而不是在每个片段中解决了这个问题。谢谢Alls