0

Activitya を使用して、作成時にsを設定したクラスにViewPager3 つFragmentの sを作成する があります。ViewPagerFragmentsetRetainInstace(true);

私の の 1 つの中に、Fragment編集可能な情報が表示されています。これにより、情報を編集するためのFragmentが起動します。DialogFragment

ユーザーが向きを変更しない場合、情報を更新して結果をビューのフラグメントに送り返すことができますが、向きを変更して、 sメソッド iInterfaceに添付されている情報を更新すると、を取得しています。DialogFragmentonAttach()NullPointerException

DialogFragment新しいものが起動されるたびにonAttach()メソッドが常に呼び出されるため、理由はわかりません。

これをどのように解決すればよいですか?インターフェイスの状態を保存できますか? もしそうなら、どのように?

ここに私の DialogFragment コードがあります: GenericDialogFragment クラスは、外観を変更するためにのみ使用されます

public class Fragment1 extends GenericDialogFragment implements WebServiceResult{



// -------------------------------------------------------------------------
// Member Variables
//--------------------------------------------------------------------------
//Webservice Callback
private WSRequest mActiveRequest = null;
// The Current Context of the Application
private Context mClassContext = null;
// Interface reference for communication
private static CommunicateResults communicateResults = null;


// -------------------------------------------------------------------------
// New Instance Method
//--------------------------------------------------------------------------    

public static Fragment1 newInstance(int userId, GenericObject [] objects, GenericGroup [] groups, Object currentObject){
    // Initialize a new Fragment1
    Fragment1 fragment = new Fragment1();
    // Create a new Bundle
    Bundle args = new Bundle();

    fragment.setArguments(args);

    // Return the Fragment1
    return fragment;
}

// -------------------------------------------------------------------------
// Class Functions / Methods
//--------------------------------------------------------------------------    

// States that the Interface is attached to the parent activity
@Override public void onAttach(Activity activity)
{
    // Perform the Default Behavior
    super.onAttach(activity);
    Log.d("ONAttach()","On attach() is called" );
    // Try 
    try{
        // Attach the interface to the activity
        communicateResults = (CommunicateResults) ((MainActivity)  getActivity()).findFragment(EditableFragment1.class);

    }catch(Exception ex){
        // Print the stack trace
        ex.printStackTrace();
    }
}

// States that the Dialog's View has been Created
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    // Return the Inflated XML Layout
    return inflater.inflate(R.layout.fragment1, container, false);
}

// States that the fragment has been created, last chance to update the UI
@Override
public void onActivityCreated(Bundle savedInstanceState){
    // Perform the Default behavior
    super.onActivityCreated(savedInstanceState); 

    mClassContext = getActivity();

    // Get the Arguments passed into the Fragment instance
    savedStateData = getArguments();

    // Reference all the UI Elements in the View    


    // Add listeners to the Button Widgets


    // If the savedInstanceState is not null, get the current object
    if(savedStateData != null){

        // Get the object out of the state data
        mCurrentObject = savedStateData.getParcelable(STATE_DATA_CURRENT_OBJECT);

    }



}


//-----------------------------------------------------------------------------
// Webservice Callback methods
//-----------------------------------------------------------------------------

// States that the web service has succeeded 
@Override public void webserviceSucceeded(WebServiceBase finishedService, Object responseData) 
{

    Log.d("EDIT Object", responseData.toString());

    if(responseData != null){

        if(mDeletingObject){



            // Send Back to the object to remove
            communicateResults.sendBackData(mCurrentObject, ACTION_DELETE);

        }else{

            JSONObject tempObject = (JSONObject) responseData;

            try{

                // Parse Data ...



            }catch(Exception ex){

                ex.printStackTrace();
                // TODO: The Object was deleted from the Lest
            }

            // If we are creating a object, bundle the information to pass to the parent activity
            if(mCreatingObject){

                // Create a new Workout Object
                mCurrentObject = new Object();


                // Callback to Parent Activity to notify that data has changed
                communicateResults.sendBackData(mCurrentObject, ACTION_CREATE);

                // Else the Object was updated
            }else{
                // Create a new  Object
                mCurrentObject = new Object();


                // Callback to Parent Activity to notify that data has changed
                communicateResults.sendBackData(mCurrentObject, ACTION_UPDATE);
            }
        }


    }

    // Dismiss the fragment


}


// States that the web service has failed
@Override
public void webserviceFailed(WebServiceBase finishedService,
        Object errorData) {


    // Display the Error
    displayErrorData(errorData);

}

}

4

1 に答える 1

0

を探していると思いますがonActivityCreated(Bundle bundle);、これはクラスのにFragment相当します。ActivityonRestoreSavedInstanceState(Bundle bundle);

ドキュメントから:

public void onActivityCreated (Bundle savedInstanceState) API レベル 11 で追加

フラグメントのアクティビティが作成され、このフラグメントのビュー階層がインスタンス化されたときに呼び出されます。ビューの取得や状態の復元など、これらの部分が配置されたら、最終的な初期化を行うために使用できます。setRetainInstance(boolean) を使用してインスタンスを保持するフラグメントにも役立ちます。これは、このコールバックが新しいアクティビティ インスタンスに完全に関連付けられていることをフラグメントに通知するためです。これは onCreateView(LayoutInflater, ViewGroup, Bundle) の後、onViewStateRestored(Bundle) の前に呼び出されます。パラメータ savedInstanceState フラグメントが以前に保存された状態から再作成されている場合、これはその状態です。

向きの変更でフラグメントが破棄された場合は、その状態を名前と値のペアとしてアクティビティの に保存し、Bundle再作成する必要がある場合はInterface、このメソッドで新しいインスタンスを作成し、それぞれのフィールドを設定するか、新しいインスタンスでパーセルブルを取得しFragmentます。

于 2014-01-16T16:17:46.187 に答える