2

我從FragmentA調用對話框片段並將一些值返回給fragmentA。現在的問題是,每當我去從相同的fragmentA的另一個fragmentB並返回它我的對話框片段值得到清除片段,對話框片段問題

當我點擊諮詢醫生的textview,一個對話框打開(圖2)。在選擇一個項目(圖2)後,返回一個值到FragmentA。圖片3是一個片段B,它在相同的活動中打開。但是,當我在十字按鈕單擊圖三和popBackStack,我的諮詢醫師值表示PIC的4

enter image description here

enter image description here

enter image description here

enter image description here

Pic 4 is an ISSUE 
清零

DialogFragment

@Override 
    public void onStart() { 
    super.onStart(); 
    getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    getDialog().getWindow().setGravity(Gravity.CENTER); 
    getDialog().setCancelable(false); 
    getDialog().setCanceledOnTouchOutside(false); 
    getDialog().closeOptionsMenu(); 

    } 

    @Override public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setRetainInstance(true); 
    } 

    @Nullable @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
     @Nullable Bundle savedInstanceState) { 
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 

    View rootView = inflater.inflate(R.layout.consultant_doc_dialog, container, false); 

    recyclerView = (RecyclerView)rootView.findViewById(R.id.recycler_view); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 
    recyclerView.setHasFixedSize(true); 
    adapter = new ConsultantDoctAdapter(getContext(),this); 
    adapter.getDocList().addAll(new ArrayList<DoctorList>()); 
    recyclerView.setAdapter(adapter); 
    adapter.getDocList().clear(); 
    adapter.getDocList().addAll(list); 
    adapter.notifyDataSetChanged(); 


    close = (ImageButton)rootView.findViewById(R.id.bt_close); 
    close.setOnClickListener(new View.OnClickListener() { 
     @Override public void onClick(View view) { 
     getDialog().dismiss(); 
     } 
    }); 
    //cityEditText.setOnQueryTextListener(onQueryTextListener); 

    return rootView; 
    } 

片段

@Nullable @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
     @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.layout_create_leads, container, false); 
    ButterKnife.bind(this, view); 
    setRetainInstance(true); 

     init(); 
     setPicker(); 
     setSpinnerListener(); 
     btCheckCalendar.setOnClickListener(this); 
     etCityId.setOnClickListener(this); 
     etConsultingDocId.setOnClickListener(this); 
     btSubmit.setOnClickListener(this); 
     tvClientReferral.setOnClickListener(this); 
     etSalesPerson.setText(sharedPref.getString(AppConstants.PREFERENCE_USER_NAME, "")); 
     etZone.setText(sharedPref.getString(AppConstants.USER_ZONE, "")); 
     etAreaCode.setText(sharedPref.getString(AppConstants.USER_AREA_CODE, "")); 
     setSpinner(); 
     getConsultantDoctorList(); 
    return view; 
    } 

片段B回調:

getActivity().getSupportFragmentManager() 
    .beginTransaction() 
    .replace(R.id.content_main, new MyCalendarFragment()) 
    .addToBackStack("calendarFragment") 
    .commit(); 

DialogCallack:

ConsultantDocDialogFragment consultantDocDialog = new ConsultantDocDialogFragment(); 
     consultantDocDialog.setParameter(getContext(), this, doclist); 
     consultantDocDialog.show(getActivity().getSupportFragmentManager(), 
      ConsultantDocDialogFragment.class.getSimpleName()); 
     break; 

請幫助我,讓我可以能保存狀態的值從對話片段了。

回答

1

請找到下面的代碼,它可以幫助你 -

這是片段的代碼,你可以從對話片段 -

HomeFragment.java

得到回調0
public class HomeFragment extends Fragment implements AlertDFragment.Callback { 
    private static final int DIALOG_FRAGMENT = 100; 
    Button alertdfragbutton; 
    private View rootView; 

    public HomeFragment() { 
    } 

    public static HomeFragment newInstance() { 
     return new HomeFragment(); 
    } 

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

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.fragment_home, container, false); 
     initUI(rootView); 
     return rootView; 
    } 

    private void initUI(View rootView) { 
     alertdfragbutton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       AlertDFragment alertdFragment = new AlertDFragment(); 
       alertdFragment.setTargetFragment(HomeFragment.this, DIALOG_FRAGMENT); 
       // Show Alert DialogFragment 
       alertdFragment.show(getChildFragmentManager(), "Alert Dialog Fragment"); 
      } 
     }); 
    } 

    @Override 
    public void accept() { 
     Log.e("Home ", "OK"); 
    } 

    @Override 
    public void decline() { 

    } 

    @Override 
    public void cancel() { 
     Log.e("Home ", "CANCEL"); 
    } 
} 

下面是對話的片段,我們有方法 -

public class AlertDFragment extends DialogFragment { 
    Callback callback; 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     callback = (Callback) getTargetFragment(); 
     return new AlertDialog.Builder(getActivity()) 
       // Set Dialog Icon 
       .setIcon(R.drawable.androidhappy) 
       // Set Dialog Title 
       .setTitle("Alert DialogFragment") 
       // Set Dialog Message 
       .setMessage("Alert DialogFragment Tutorial") 

       // Positive button 
       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         callback.accept(); 
         // Do something else 
         //getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, getActivity().getIntent()); 
        } 
       }) 

       // Negative Button 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         callback.cancel(); 
         // Do something else 
         // getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent()); 
        } 
       }).create(); 
    } 

    public static interface Callback { 
     public void accept(); 

     public void decline(); 

     public void cancel(); 
    } 
} 
0

而不是使用「片段轉型」你爲什麼不只是彈出自定義視圖 剛剛創建的

Dialogue dialogue 
View popupView 

一個全球性的基準,並在任何TextView中點擊按鈕等 可以只是調用一個方法類似

void popup(){ 
    popupView = LayoutInflater.from(getActivity()).inflate(R.layout.your_calenderlayout, null); 
    //suppose you have TextView cal_textview in popUp view i.e, your_calenderlayout 
    cal_textview = (TextView) popupView.findViewById(R.id.cal_textview); 
    dialog   = new Dialog(getContext()); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setCanceledOnTouchOutside(true); 
    dialog.setContentView(popupView); //and just add your popUpview 
    // For setting backgroung 
    /*dialog.getWindow().setBackgroundDrawableResource(android.R.color.Transparent); 
     */ 
    //For setting the width or height of popup  
    /*WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 

    lp.copyFrom(dialog.getWindow().getAttributes()); 

    lp.width = WindowManager.LayoutParams.WRAP_CONTENT; 

    lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 

    lp.gravity = Gravity.CENTER; 
    dialog.getWindow().setAttributes(lp);*/ 
    dialog.show(); 
    } 

和解僱彈出窗口或內部popupView視圖點擊你設置的變量或成員的值的片段內直接

希望這將有助於

+0

聲明回調不,我想用R.Layout.calendar_layout作爲B片段 –

1

一個簡單的方法從DialogFragment返回值是使用setTargetFragment用於調用fragmentB創作,然後將數據返回到getTargetFragment(如果不爲null)。在fragmentA中,您可以通過onActivityResult接收數據。

另一種方法是使用SharedPreferences。您可以通過onResumeonHiddenChanged獲得新值。

+0

SharedPrefences ....我收到來自對話框有點8個字段值,一些來自datepicker/timepicker,並且所有人在返回時都清除。因此,你仍然建議使用Shared Pref並在那裏保存這麼多值。 –

+1

@ young_08好吧,如果你認爲它是真的,那麼它使用共享首選項8個值不是一個大問題,那麼你可以使用SQLite或遵循其他建議的解決方案CoolMind –

+0

@AdeelTurk所以我嘗試過其他解決方案CoolMind這是setTargetFragment。面對同樣的問題,當我從片段B回到片段A時,我在醫生顧問領域設置的getTargetFragment值就清除了。這隻會發生在對話框和日期選擇器上。其他領域保留價值。 ): –

0

,可以使用共享的首選項或sqlite的讓你的價值觀,如果你認爲它使用較少保存在共享的首選項或sqlite的臨時數據然後辛格爾頓模式是一個不錯的選擇。我認爲,我們應該遵循KISS 設計原則:P