2013-05-10 39 views
6

Tôi có giao diện này trong hoạt động của mình.Cách gọi lại giữa Hoạt động và Phân đoạn?

public interface LogoutUser { 
    void logout(); 
} 

đoạn tôi thực hiện giao diện này, vì vậy trong đoạn của tôi, tôi có điều này:

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

Trong hoạt động của tôi, tôi gọi

mLogoutUser.logout(); 

đâu mLogoutUser là của giao diện kiểu LogoutUser.

Vấn đề của tôi là đối tượng mLogoutUser không có giá trị. Làm thế nào có thể khởi tạo nó?

Cảm ơn bạn!

+1

mLogoutUser = yourFragment; – dymmeh

Trả lời

3

Android Fragments - Communicating with Activity

Bạn cần phải nhận được một tham chiếu đến đoạn bạn với getFragmentById() hoặc getFragmentByTag()

getFragmentManager().findFragmentById(R.id.example_fragment); 
+4

thanx. một cách khác là sử dụng onAttach() – androidevil

+0

@androidevil cách sử dụng onAttach()? – DroidLearner

9

Như tôi đã nói trong nhận xét của tôi, tôi giải quyết vấn đề này bằng onAttach phương pháp trong đoạn của tôi, nhưng trong này cách bạn phải có trường gọi lại (mLogoutUser trong trường hợp này) được khai báo trong phân đoạn và khởi tạo theo cách này:

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"); 
     } 
    } 

    ... 
} 

Thông tin thêm trong Communicating with Other Fragments.


Nhưng nếu trường hợp của bạn là lĩnh vực khai báo trong hoạt động này, bạn có thể sử dụng phương pháp onAttachFragment từ hoạt động của bạn để khởi tạo lĩnh vực nghe bạn theo cách này:

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

    mLogoutUser = (LogoutUser) fragment; 
} 

Ngoài ra, bạn có thể sử dụng một xe buýt sự kiện để làm cho sự giao tiếp này giữa các mảnh vỡ và các hoạt động. Một tùy chọn là Otto library, từ Square.

+1

Cảm ơn một tấn @androidevil. Bạn đã cứu ngày của tôi! BTW nếu chúng ta có nhiều phần đính kèm, chúng ta có thể nếu (fragment instanceof fragmentone) {} trong onAttachFragment. – Kishore

4

mẫu cho việc tạo ra gọi lại từ Fragment để Hoạt động

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

Vô cùng hữu ích ... cảm ơn bạn rất nhiều Nepster – JamisonMan111

+0

Cảm ơn bạn đã trả lời ví dụ đầy đủ này! –

Các vấn đề liên quan