Android:Fragment完成一个Activity并且不调用onActivityResult

时间:2013-11-22 14:05:11

标签: android android-fragments android-activity activity-finish onactivityresult

我搜索并发现了大量的onActivityResult()/startActivityForResult()问题,但我还没有解决我的问题。

在我的应用中,我有一个MainActivity应该调用EnterActivity。 EnterActivity有两个片段,一个SignupFragment和一个LoginFragment。目前,MainActivity使用startActivityForResult()启动EnterActivity,它也会覆盖onActivityResult()

从SignupFragment / LoginFragment,我调用getActivity().setResult(...)getActivity().finish(),完成EnterActivity,但永远不会调用MainActivity.onActivityResult()。 有人可以帮忙吗?

public class MainActivity extends Activity {
    ...

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (666 == requestCode) {
            Log.in("gotcha!");
        }
    }

    @Override
    protected void onCreate(Bundle bundle) {
        Intent intent = new Intent(this, EnterActivity.class);
        startActivityForResult(intent, 666);
    }
}

子活动如下所示:

public class EnterActivity extends ActionBarActivity {
    ...
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.main_frame, new LoginFrament())
            .commit();
    }
}

Fragment看起来像:

public class LoginFragment extends Fragment {
    ...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        getView().findViewById(R.id.buttonSignUpLogIn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ...
                getActivity().setResult(Activity.RESULT_OK);
                getActivity().finish();
            }
        });
    }
}

0 个答案:

没有答案