Android:片段之间的onActivityResult()没有按预期工作

时间:2017-01-04 09:02:28

标签: android android-fragments

这是从Fragment B开始Fragment A的代码。

private void gotoFragmentB() {
    FragmentB fragment = new FragmentB();
    fragment.setTargetFragment(this, CODE_PATTERN_AND_PIN);
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.addToBackStack(null);
    transaction.replace(R.id.main_frame, fragment);
    transaction.commit();
}

这是从Fragment C开始Fragment B的代码。

private void gotoFragmentC(String pattern) {
    Bundle bundle = new Bundle();
    bundle.putString(Keys.VALUE, pattern);
    bundle.putInt(Keys.OPTION, Keys.OPTION_PIN_AND_PATTERN);
    FragmentC fragment = new FragmentC();
    fragment.setTargetFragment(this, getTargetRequestCode());
    fragment.setArguments(bundle);
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.main_frame, fragment);
    transaction.commit();
}

最后,此代码从Fragment A转到Fragment C

private void gotoFragmentA(String pin, String pattern) {
    Intent intent = new Intent();
    intent.putExtra(Keys.VALUE, pin);
    intent.putExtra(Keys.PATTERN, pattern);
    getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_OK, intent);
    Log.i(ApplicationManager.LOG, "The request code is " + getTargetRequestCode());
    Log.i(ApplicationManager.LOG, "Sending " + pin + " and " + pattern);
    getFragmentManager().popBackStack();
    closeSoftKeyboard();
}

根据上述代码,我成功地将数据从Fragment A传递到Fragment B,并从Fragment B传递到Fragment C。但事实是,我未能将数据从Fragment A带回Fragment C

在上一个代码段中,我收到如下Log消息。

01-04 17:52:12.316 18694-18694/com.m I/MyAlarmApp: The request code is 103
01-04 17:52:12.326 18694-18694/com.m I/MyAlarmApp: Sending 1234 and 004-000&003-001&002-002&001-003&000-004&001-004&002-004&003-004&004-004

所以,我相信可能是现在错误编码的onActivityResult()方法。到目前为止,这是一个。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT_OK && data != null) {
        switch(requestCode) {

            case CODE_PATTERN_AND_PIN:
                alarmPIN = data.getStringExtra(Keys.VALUE);
                pattern = data.getStringExtra(Keys.PATTERN);
                Log.i(ApplicationManager.LOG, "PIN set to " + alarmPIN);
                itemAlarmLock.setSubtitle(getString(R.string.pattern));
                break;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

只需使用侦听器(接口)在片段之间传递信息。 OnActivityResult 在活动通信活动中会更有用。

示例:假设您有两个片段A,B和一个活动,您需要将数据从B传递到A. 然后在B中创建一个接口,并将数据传递给Main Activity。创建另一个接口并将数据传递给片段A.

片段A应如下所示:

public class FragmentA extends Fragment implements InterfaceDataCommunicatorFromActivity {
   public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity;
   String data;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void updateData(String data) {
    // TODO Auto-generated method stub
    this.data = data;
    //data is updated here which is from fragment B
}

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    try {
        interfaceDataCommunicatorFromActivity = (InterfaceDataCommunicatorFromActivity) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement TextClicked");
    }

}

}

FragmentB应如下所示:

class FragmentB extends Fragment {
public InterfaceDataCommunicator interfaceDataCommunicator;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    // call this inorder to send Data to interface
    interfaceDataCommunicator.updateData("data");
}

public interface InterfaceDataCommunicator {
    public void updateData(String data);
}

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    try {
        interfaceDataCommunicator = (InterfaceDataCommunicator) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement TextClicked");
    }

}

}

和主要活动应该看起来:

public class MainActivity extends Activity implements InterfaceDataCommunicator {
public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void updateData(String data) {
    // TODO Auto-generated method stub
    interfaceDataCommunicatorFromActivity.updateData(data);

}

public interface InterfaceDataCommunicatorFromActivity {
    public void updateData(String data);
}

}

答案 1 :(得分:0)

您应尝试在特定片段的活动中覆盖 onActivityResult

如果您的片段的onActivityResult不起作用,请尝试使用以下代码。

在你的主要活动



@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Fragment pFragment = getSupportFragmentManager().findFragmentById(R.id.container);
        if (pFragment instanceof FragmentA) {
            if (pFragment != null) {
                pFragment.onActivityResult(requestCode, resultCode, data);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }




通常你可以在片段中使用onActivityResult方法



@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }