how to confirm quit application in fragment ? when user press back key

℡╲_俬逩灬. 提交于 2019-12-24 15:19:19

问题


i am creating an applicaiton with different fragments. I want to add code to listener for back key event and confirm with user if he wants to exit the app or not..

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      



}

I used the following code for activity and it works fine.. but it does not work the fragment file above.

    // 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);
}

when i copy this code the above fragment .. i got error of : The method onKeyDown is undefiled for type Fragment..


回答1:


I have solved the issue by adding the above code in the Main Activity not in each Fragment. Thanks Alls



来源:https://stackoverflow.com/questions/26850104/how-to-confirm-quit-application-in-fragment-when-user-press-back-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!