显示片段中Intent的数据

时间:2013-09-14 16:53:50

标签: android android-intent android-fragments

我有一个接收意图的活动,然后应该在嵌套片段中显示意图的内容。我的代码与实施有效导航教程相同,后者是here,下面有一些修改

如示例所示,片段嵌套在主活动中

public static class DummySectionFragment extends Fragment {
    ....
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
    ...
    public void updateFragUI() {

        if(rootView!=null){
            ((TextView) rootView.findViewById(R.id.example)).setText(mData.getSomething());                 
     }

我在获取片段实例时遇到困难,以便在MainActivity收到意图后我可以更新UI。接收意图和更新片段的代码是

    public class uiReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        mData = getIntent().getParcelableExtra(ExampleService.EXAMPLE_INTENT);
        updateUI(mData);
    }
}



public void updateUI() {
        DummySectionFragment dummyFrag = (DummySectionFragment)
            getSupportFragmentManager().findFragmentById(dummyFragId);

    if(dummyFrag==null) {
        Log.v(TAG,"Dummy frag is null");
    } else {
        if(dummyFrag.isVisible()) {
            Log.v(TAG,"Dummy frag is visable ");
                            dummyFrag.updateFragUI();
        } else {
            Log.v(TAG,"Dummy frag is not visable");
        }
    }

    }

我尝试了一些使用变量dummyFragId的方法,但我总是发现dummyFrag始终为空。到目前为止,我已经尝试过:

  1. 尝试片段的XML代码中的标签和ID。即dummFragId被写为R.id.dummy_fragment_id(或代码),其中包含<FrameLayout ...
  2. 中的相应属性
  3. 从片段事务中获取片段标记,但这不是在有效的导航代码中明确完成的,即。
  4. 使用dummyFragId = dummySectionFragment.getId()获取片段ID,即

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                // The first section of the app is the most interesting -- it offers
                // a launchpad into the other demonstrations in this example application.
                return new LaunchpadSectionFragment();
    
            case 1:
                Fragment dummySectionFragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
                dummySectionFragment.setArguments(args);
                return dummySectionFragment;
    
  5. 在片段中注册接收者。 (接收器是我无法更新UI的外部类)

  6. 我对解决方案持开放态度,我只是想知道从片段中的意图中显示信息的最佳方式。

1 个答案:

答案 0 :(得分:0)

我认为通过在片段中创建一个寄存器方法,我找到了一个很好的解决方案。活动中没有相关代码,它都在片段中。希望这有助于某人。

public static class DummySectionFragment extends Fragment {
    public void updateFragUI() {
        Log.v(TAG, "updateFragUI received");
        ((TextView) getView().findViewById(R.id.example_field))
                .setText(Double.toString(mData.getSomething()));
    }


    private IntentFilter filter = new IntentFilter(
            TransmittingService.STATE_UPDATE);

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mData = intent
                    .getParcelableExtra(TransmittingService.STATE_VALUES);
            updateFragUI();
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        Log.v(TAG, "registering Receiver");
        getActivity().registerReceiver(mReceiver, filter);
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.v(TAG, "unregistering receiver");
        getActivity().unregisterReceiver(mReceiver);
    }
...
}