0

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

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

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. dummyFragId = dummySectionFragment.getId()使用ie获取片段 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;
    
  4. 在片段中注册接收器。(接收器是一个外部类,我无法从中更新 UI)

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

4

1 回答 1

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);
    }
...
}
于 2013-09-15T11:22:34.870 回答