活动通过回调将数据传递给片段

时间:2013-11-18 04:19:58

标签: android android-activity android-fragments callback android-fragmentactivity

我在这里遇到了一个情况。

FragmentActivty持有Fragment。当我点击Fragment中的按钮时,我将转到Activty。当我到达我做某事的活动时,这将影响我来自的Fragment中显示的数据。因此,为了在片段中进行更改,我想提供从ActivityFragment的回调。首先我想实现onActivityResult。但我意识到这不是我需要的。

我的做法是错的吗?请指导我

MyActivity extends FragmentActivity

MyActivity持有

MyFragment extends Fragment

从这里开始,我要去

SecondActivity extends Activity

来自SecondActivity我需要回复MyFragment。我的方法有什么问题吗?

编辑:

 public class MainActivity extends FragmentActivity {
    private FrameLayout frameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frameLayout = (FrameLayout) findViewById(R.id.framelayout);
         loadFragment();

    }

    @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;
    }

    private void loadFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        MyFragment myFragment = new MyFragment();
        fragmentTransaction.add(R.id.framelayout, myFragment);
        fragmentTransaction.commit();

    }
}

MyFragment.java

public class MyFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view, container, false);
        Button button = (Button) view.findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                callActivity();

            }
        });

        return view;
    }



    private void callActivity() {
        Intent intent = new Intent(getActivity(), SecondActivity.class);
        startActivityForResult(intent, 10);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//      super.onActivityResult(requestCode, resultCode, data);
        Log.e("MyFragment Inside", "Onresultttt");
        if (requestCode == 10) {
            if (resultCode == Activity.RESULT_OK) {
                Log.e("Result code", Activity.RESULT_OK + " okkk");
            }
            if (resultCode == Activity.RESULT_CANCELED) {
                Log.e("Result code", Activity.RESULT_CANCELED + "cancelll inside fragment");
            }
        }
    }
}

SecondActivity.java

public class SecondActivity extends Activity {
    private static final String TAG = "second activity";

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

    @Override
    public void onBackPressed() {
        // super.onBackPressed();
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        Log.e(TAG, "result setted");
        finish();

    }
}

2 个答案:

答案 0 :(得分:1)

如果我正确了解您的情况,那么处理通信的正确方法是让SecondActivity将信息传递给MyActivity,MyActivity将直接配置它的MyFragment实例。除非将片段附加到活动,否则您不希望从活动访问片段。

至于如何进行通信,正如您所建议的那样,一种方法是使用startActivityForResult()。有关详细信息,请参阅此答案:How to manage `startActivityForResult` on Android?

关于startActivityForResult()的注释。如果您从片段中调用它,那么您的片段将收到结果,而不是您的活动。从Fragment调用startActivityForResult()还有一些其他问题,所以我通常建议您从Activity中调用它,从而处理Activity中的结果。

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, INTENT_CODE);

从片段到活动进行通信的最佳实践方法是在片段中定义一个接口,该活动实现。

public class MyFragment extends Fragment {
    public interface MyFragmentListener() {
        public void onMyFragmentEvent();
    }

    public void startTheActivityForResult() {
        ((MyFragmentListener)getActivity()).onMyFragmentEvent();
    }
}

public class MainActivity extends FragmentActivity implements MyFragmentListener{
    @Override public void onMyFragmentEvent() {

    }
}

Fragment然后只是将引用转换为它所附加的Activity,因为知道Activity必须实现Listener,因此允许你在其他Activity中重用Fragment。

答案 1 :(得分:1)

请参阅此问题,它将帮助您了解正在发生的事情: onActivityResult is not being called in Fragment