我有一个Fragment需要将多个Action传回给它的Activity。例如,
2.当用户的登录名和密码匹配时,会向活动发送一个布尔值,通知它启动一个意图。
我的第一个问题是,这是常见的,片段需要将一种类型的Action转发回Activity吗?其次,这是如何解决的?以下是一个很好的方法...
我创建了一个自定义类,它扩展了Fragment并包含了我需要的两个接口(一个将onClick传递回Activity,一个传递一个布尔值):
public class CustomInterfaceFragment extends Fragment {
public OnClickedListener listener;
public LogInInterface loggedInListener;
static interface OnClickedListener{
public void buttonClicked(View v);
}
static interface LogInInterface{
public void userLoggedIn(boolean loggedIn);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.listener = (OnClickedListener)activity;
this.loggedInListener = (LogInInterface)activity;
}}
然后我在Fragment中扩展了这个自定义类,并在需要时使用了适当的方法。这是片段中的onClick方法......
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.register_button:{
listener.buttonClicked(v);//***Pass onClick Back to Activity
break;
}
case R.id.fragment_login_loginButton:{
ParseUser.logInInBackground(userName.getText().toString(), password.getText().toString(), new LogInCallback() {
@Override
public void done(ParseUser user, ParseException e) {
if (user!=null){
boolean verified = user.getBoolean("emailVerified");
if(!verified){
Toast.makeText(getActivity(),"Please Verify",Toast.LENGTH_LONG).show();
progressDialog.dismiss();
ParseUser.logOut();
}else{
progressDialog.dismiss();
loggedInListener.userLoggedIn(true);//***Pass boolean Back to Activity
}
}else {
Toast.makeText(getActivity(),e.getMessage(),Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
});
}
break;
}
}
最后,我在Activity中实现了自定义片段类及其接口,以便检索数据。
这是解决这个问题的合理方法还是我错过了什么?该应用程序似乎工作正常。我只是想知道最好的编程实践是什么。谢谢。
答案 0 :(得分:1)
我只能说你可以将这两个接口关闭到下面这样的接口
public interface fragmentInteractions{
public void OnClickedListener(View v);
public void userLoggedIn(boolean loggedIn);
....
....
}
我不认为这里的界面需要是静态的
答案 1 :(得分:0)
阐述阿维纳什乔希的回答:
public interface CustomListener {
void onButtonClicked();
void onLoginResult( boolean isUserLoggedIn ); // You can pass User object via this method in case its required to do some operations
}
public class MainActivity extends Activity implements CustomListener {
@Override
public void onCreate( Bundle savedInstance ) {
// Initialize UI elements
// Initialize Fragment
}
@Override
public void onButtonClicked() {
//Action to be performed on button click
}
@Override
public void onLoginResult( boolean isUserLoggedIn ) {
if( isUserLoggedIn ) {
//take user to dashboard or any other screen
//Usually with the help of SupportFragmentManager
}
else {
//Take user to signup screen with an optional toast message
//In case parameters like User name and password need not be entered by user again, you can access them as function parameters and pass them to signupFragment via bundle
}
}
}
public class LoginFragment extends Fragment {
CustomListener mCustomListener;
@Override
public void onAttach( Context context ) {
super.onAttach( Context context );
try {
mCustomListner = (CustomListener) context;
} catch ( ClassCastException e {
Log.e(TAG, "Activity must implement CustomListener")
}
}
//Rest of Fragment initialization code here
}
这是一个完整的例子:
http://www.truiton.com/2015/12/android-activity-fragment-communication/