2013-05-10 11 views
6

Mam ten interfejs w mojej działalności.Jak nawiązać połączenie między działaniem a fragmentem?

public interface LogoutUser { 
    void logout(); 
} 

Moja fragment implementuje ten interfejs, więc w moim fragment, mam to:

@Override 
public void logout() { 
    // logout 
} 

W mojej działalności wzywam

mLogoutUser.logout(); 

Gdzie mLogoutUser jest interfejs typu LogoutUser.

Mój problem to obiekt mLogoutUser, który ma wartość NULL. Jak można go zainicjować?

Dziękujemy!

+1

mLogoutUser = yourFragment; – dymmeh

Odpowiedz

9

Jak powiedziałem w moim komentarzu, ja rozwiązany ten problem przy użyciu onAttach metodę w moim fragmentu, ale w tym sposób musi mieć pole wywołania zwrotnego (mLogoutUser w tym przypadku) zadeklarowane w fragmencie i zainicjować go w ten sposób:

public class MyFragment extends ListFragment { 
    LogoutUser mLogoutUser; 

    // Container Activity must implement this interface 
    public interface LogoutUser { 
     public void logout(); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mLogoutUser = (LogoutUser) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement LogoutUser"); 
     } 
    } 

    ... 
} 

Więcej informacji pod numerem Communicating with Other Fragments.


Ale jeśli sprawa jest pole zadeklarowane w działaniu, można użyć metody onAttachFragment ze swojej działalności w celu zainicjowania swojej dziedzinie słuchacz w ten sposób:

@Override 
public void onAttachFragment(Fragment fragment) { 
    super.onAttachFragment(fragment); 

    mLogoutUser = (LogoutUser) fragment; 
} 

Można także wykorzystaj magistralę zdarzeń do wykonania tej komunikacji między fragmentami i działaniami. Opcją jest Otto library, od kwadratu.

+1

Dzięki tonie @androidevil. Uratowałeś mój dzień! BTW jeśli mamy wiele załączonych fragmentów, możemy ci, jeśli (fragment instanceof fragmentu) {} w onAttachFragment. – Kishore

4

próbki do tworzenia zwrotnego z fragmentu aktywność

public interface CallBackListener { 
    void onCallBack();// pass any parameter in your onCallBack which you want to return 
} 

CallBackFragment.class

public class CallBackFragment extends Fragment { 

    private CallBackListener callBackListener; 

    public CallBackFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     return inflater.inflate(R.layout.fragment_call_back, container, false); 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     //getActivity() is fully created in onActivityCreated and instanceOf differentiate it between different Activities 
     if (getActivity() instanceof CallBackListener) 
      callBackListener = (CallBackListener) getActivity(); 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     Button btn = (Button) view.findViewById(R.id.btn_click); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(callBackListener != null) 
        callBackListener.onCallBack(); 
      } 
     }); 
    } 
} 

CallbackHandlingActivity.class

public class CallbackHandlingActivity extends AppCompatActivity implements CallBackListener 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_all_user); 

    } 

    @Override 
    public void onCallBack() { 
     Toast.makeText(mContext,"onCallback Called",Toast.LENGTH_LONG).show(); 
    } 
} 
+1

Niezwykle pomocny ... bardzo dziękuję Nepster – JamisonMan111

+0

Dziękuję za tę pełną przykładową odpowiedź! –

Powiązane problemy