2013-12-17 69 views
8

所以我有一個DialogFragment顯示區域選擇列表。用戶點擊該區域後,列表將重新提取數據以顯示街道選擇列表。在這一點上,我想讓用戶按硬件返回按鈕返回到區域選擇。這可能嗎?我試圖覆蓋下面的一些方法,但我只能點擊事件,但不能阻止它發生。處理DialogFragment行爲的最佳方法是什麼?

@Override 
public void onCancel(DialogInterface dialog) { 
    if(isDismissable()){ 
     super.onCancel(dialog); 
    }else { 
     Log.d(TAG, "Don't dismiss cancel this dialog!"); 
    } 
} 

@Override 
public void dismissAllowingStateLoss() { 
    if(isDismissable()){ 
     super.dismissAllowingStateLoss(); 
    }else { 
     Log.d(TAG, "Don't dismiss this dialog!"); 
    } 
} 

@Override 
public void dismiss() { 
    if(isDismissable()){ 
     super.dismiss(); 
    }else { 
     Log.d(TAG, "Don't dismiss this dialog!"); 
    } 
} 

dismiss()被調用時,用戶按下返回鍵,但即使我不叫super.dismiss(),對話獲得不管駁回。

有沒有辦法做到這一點?我還會研究如何Google+應用程序在DialogFragment中顯示ActionBar以提供HomeAsUp,但我無法找到任何信息。

回答

5

我看到了兩個解決方案:

最簡單的:擁有區域選擇和街道選擇列表作爲兩個獨立的平常片段,並有他們兩個在一個單獨的活動,並通過一個簡單的主題有這個活動作爲一個對話框: <activity android:theme="@android:style/Theme.Dialog" />並且excludeFromRecents="true"在最近使用過的應用程序中沒有這個。 先選擇區域選擇,然後通過addToBackStack(null)添加街道選擇,以便在下面有AreaSelection片段。

如果您不想因爲任何原因爲此單獨進行任何活動,則可以從dialogfragment及其實現者(活動)中添加一個對話框偵聽器,以打開AreaFragment。隨着你的代碼的基本理解這個簡單的項目應該這樣做:

所有者活動:

import com.example.adip.fragments.AreaSelectionFragment; 
import com.example.adip.fragments.StreetSelectionFragment; 
import com.example.adip.fragments.AreaSelectionFragment.AreaSelectionListener; 
import com.example.adip.fragments.StreetSelectionFragment.StreetSelectionListener; 

import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.support.v4.app.FragmentActivity; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class DialogsActivity extends FragmentActivity implements OnClickListener, 
     AreaSelectionListener, StreetSelectionListener { 

    private static final String AREA_TAG = "AREA_TAG"; 

    private static final String STREETS_TAG = "STREETS_TAG"; 

    @Override 
    protected void onCreate(Bundle savedInstance) { 
     super.onCreate(savedInstance); 
     setContentView(R.layout.area_selections); 
     findViewById(R.id.btnStuff).setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     showArea(); 
    } 

    private void showArea() { 
     DialogFragment df = new AreaSelectionFragment(); 
     df.show(getSupportFragmentManager(), AREA_TAG); 
    } 

    @Override 
    public void onStreetsUserCanceled() { 
     showArea(); 
    } 

    @Override 
    public void showStreets() { 
     DialogFragment df = new StreetSelectionFragment(); 
     df.show(getSupportFragmentManager(), STREETS_TAG); 
    } 

} 

AreaSelectionFragment(其擴展到您的需要):

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 

public class AreaSelectionFragment extends DialogFragment { 
    public static interface AreaSelectionListener { 
     void showStreets(); 
    } 

    private AreaSelectionListener areaSelectionListener; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     if (activity instanceof AreaSelectionListener) { 
      areaSelectionListener = (AreaSelectionListener) activity; 
     } else { 
      throw new ClassCastException("Parent Activity must implement AreaSelectionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     areaSelectionListener = null; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     return new AlertDialog.Builder(getActivity()).setTitle("Area Selection") 
       .setPositiveButton("OK", new OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         areaSelectionListener.showStreets(); 
        } 
       }).setNegativeButton("Cancel", null).create(); 
    } 
} 

而且StreetSelectionFragment(再次:擴展到您的需求):

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 

public class StreetSelectionFragment extends DialogFragment { 
    public static interface StreetSelectionListener { 
     void onStreetsUserCanceled(); 
    } 

    private StreetSelectionListener selectionListener; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     if (activity instanceof StreetSelectionListener) { 
      selectionListener = (StreetSelectionListener) activity; 
     } else { 
      throw new ClassCastException("Parent activity must implement StreetSelectionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     selectionListener = null; 
     super.onDetach(); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     Dialog dialog = new AlertDialog.Builder(getActivity()).setTitle("Street Selection") 
       .setPositiveButton("OK", null).setNegativeButton("Cancel", new OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         selectionListener.onStreetsUserCanceled(); 
        } 
       }).create(); 
     return dialog; 
    } 

    @Override 
    public void onCancel(DialogInterface dialog) { 
     super.onCancel(dialog); 
     selectionListener.onStreetsUserCanceled(); 
    } 
} 
+0

這兩種解決方案都不好...... 1.如果您在嵌套片段中工作,則不起作用; 2.將啓動一個新的對話框,以便在內容更改時無法進行任何良好的過渡/動畫。 – Ixx

3

處理DialogFragment行爲的最佳方法是什麼?

處理上DialogFragment後面的行爲將是不惹它,離開它,因爲它是,在這個過程中重新思考你目前的方法的最佳辦法。如果我在應用程序中看到對話框,並且我正在回擊,我期望彈出對話框不在屏幕上,並且不在對話框頁面之間導航(我希望我沒有誤讀您的問題) 。

有沒有辦法做到這一點?

在你DialogFragment您可以使用自定義Dialog(我假設你只是使用onCreateDialog()回調返回Dialog與列表),而您覆蓋onKeyDown()回調來處理回壓:

public class CustomDialog extends Dialog { 

    protected CustomDialog(Context context) { 
     super(context); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
      // the back key was pressed so do something? 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

} 

我也研究如何Google+應用在 DialogFragment顯示的是動作條提供HomeAsUp,但我找不到它的任何信息。

您總是可以使Activity看起來像Dialog

+0

謝謝。我使用DialogFragment,因爲我可能會將它用作平板電腦版的片段。現在我正在使用DialogFragment頂部的仿ActionBar。 – RobGThai

1
The best way is to override onBackPressed() in the dialog , you created in onCreateDialog(). 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    return new Dialog(getActivity(), getTheme()){ 
     @Override 
     public void onBackPressed() { 
      //do your stuff 
     } 
    }; 
} 
相關問題