且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何确认片段中的退出申请?当用户按下返回键

更新时间:2023-12-03 10:03:46

我已通过将上述代码添加到Main Activity中而不是在每个Fragment中解决了该问题.谢谢大家

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..

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