用相同的片段

时间:2018-06-12 06:10:46

标签: android android-fragments

我正在使用工具栏中不同项目的下拉菜单。在活动中,我会在单击菜单项后立即添加片段。调用片段 OnCreateview ,并从API获取数据。对于所有菜单项,获取数据的逻辑保持相同,但只有API端点不同。所以我试图通过API端点名称传递Bundle并对所有项目使用相同的片段。但问题是 OnCreateView 只是第一次被调用,并且只有第一个片段事务才会发出请求,即使我为不同的项目点击替换了相同的片段。

Activity.java

    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    switch (i) {
        case 0: //Clients
            Bundle bundle1 = new Bundle();
            bundle1.putString("hash-key","item1");
            ReportCategoryFragment rp1 = new ReportCategoryFragment();
            rp1.setArguments(bundle1);
            replaceFragment(rp1,false,R.id.container);
            break;

        case 1:
            Bundle bundle2 = new Bundle();
            bundle2.putString("hash-key","item2");
            ReportCategoryFragment rp2 = new ReportCategoryFragment();
            rp2.setArguments(bundle2);
            replaceFragment(rp2,false,R.id.container);
            break;
      }
     }

ReportCategoryFragment

    @Nullable
@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_runreport, container, false);
    setHasOptionsMenu(true);
    ButterKnife.bind(this, rootView);
    presenter.attachView(this);
    reportType = getArguments().getString("hash-key");
    Log.v("hashkey",reportType);
    presenter.fetchCategories(reportType, false, true);

    return rootView;
}

replaceFragment Function

     public void replaceFragment(Fragment fragment, boolean addToBackStack, int containerId) {
    invalidateOptionsMenu();
    String backStateName = fragment.getClass().getName();
    boolean fragmentPopped = getSupportFragmentManager().popBackStackImmediate(backStateName,
            0);

    if (!fragmentPopped && getSupportFragmentManager().findFragmentByTag(backStateName) ==
            null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(containerId, fragment, backStateName);
        if (addToBackStack) {
            transaction.addToBackStack(backStateName);
        }
        transaction.commit();
    }
}

编辑: FragmentnewInstance方法

     public static ReportCategoryFragment newInstance() {
    ReportCategoryFragment fragment = new ReportCategoryFragment();
    Bundle bundle = new Bundle();
    fragment.setArguments(bundle);
    return fragment;
}

2 个答案:

答案 0 :(得分:0)

尝试在fragment中使用静态方法来创建片段的新实例。

public static Fragment newInstance() 
{
    MyFragment myFragment = new MyFragment();
    return myFragment;
}

答案 1 :(得分:0)

简单的解决方案是使用 Broadcast Receiver fragment

中声明此内容
BroadcastReceiver broadCastNewMessage = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           //extract our message from intent
            String msg_for_me = intent.getStringExtra("some_msg");

        }
    };

现在在onCreate()片段中注册

registerReceiver(this.broadCastNewMessage, new IntentFilter("update_fragment"));

并在onDestroyView()

unregisterReceiver(broadCastNewMessage);

现在从服务类调用此方法,您希望从菜单选择中更新活动

Intent intent = new Intent("update_fragment");
intent.putExtra("some_msg", message); 
sendBroadcast(intent);