编辑:当我刚开始使用Android应用程序开发时,这个问题是作为我的第一个AndroID项目之一创建的.我保留这个是出于历史原因,但您应该考虑使用EventBus或RxJava.这是一个巨大的混乱.
请不要考虑使用它.谢谢.
事实上,如果你想要一些很酷的东西来解决使用带有多个“片段”的单个活动的问题,那么将flowless与自定义视图组一起使用.
我已经实现了一种方法来启动Fragments的创建,从Fragments使用广播意图通过LocalbroadcastManager来告诉Activity要实例化什么片段.
我知道这是一个非常长的代码,但我不是要求调试,它按照我的意图完美地工作 – 接收数据,创建可以通过Bundles参数化,而Fragments不直接实例化其他片段.
public abstract class FragmentCreator implements Parcelable{public static String fragmentCreatorKey = "fragmentCreator";public static String fragmentCreationbroadcastMessage = "fragment-creation";public static String fragmentDialogCreationbroadcastMessage = "fragment-dialog-creation";protected Bundle arguments;protected Boolean hasBundle;public FragmentCreator(Bundle arguments, boolean hasBundle){ this.arguments = arguments; this.hasBundle = hasBundle;}protected FragmentCreator(Parcel in){ hasBundle = (Boolean) in.readSerializable(); if (hasBundle == true && arguments == null) { arguments = in.readBundle(); }}public Fragment createFragment(){ Fragment fragment = instantiateFragment(); if (arguments != null) { fragment.setArguments(arguments); } return fragment;}protected abstract Fragment instantiateFragment();@OverrIDepublic int describeContents(){ return 0;}@OverrIDepublic voID writetoParcel(Parcel dest, int flags){ dest.writeSerializable(hasBundle); if (arguments != null) { arguments.writetoParcel(dest, 0); }}public voID sendFragmentCreationMessage(Context context){ Intent intent = new Intent(FragmentCreator.fragmentCreationbroadcastMessage); intent.putExtra(FragmentCreator.fragmentCreatorKey, this); LocalbroadcastManager.getInstance(context).sendbroadcast(intent);}public voID sendDialogFragmentCreationMessage(Context context){ Intent intent = new Intent(FragmentCreator.fragmentDialogCreationbroadcastMessage); intent.putExtra(FragmentCreator.fragmentCreatorKey, this); LocalbroadcastManager.getInstance(context).sendbroadcast(intent);}}
这样,创建的Fragment如下所示:
public class TemplateFragment extends Fragment implements GetActionbarTitle, VIEw.OnClickListener{ private int TitleID;public TemplateFragment(){ TitleID = R.string.app_name;}@OverrIDepublic int getActionbarTitleID(){ return TitleID;}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState){ VIEw rootVIEw = inflater.inflate(R.layout.fragment_template, container, false); return rootVIEw;}@OverrIDepublic voID onVIEwCreated(VIEw vIEw, Bundle savedInstanceState){ super.onVIEwCreated(vIEw, savedInstanceState);}@OverrIDepublic voID onClick(VIEw v){}public static class Creator extends FragmentCreator{ public Creator() { super(null, false); } public Creator(Bundle bundle) { super(bundle, true); } protected Creator(Parcel in) { super(in); } @OverrIDe protected Fragment instantiateFragment() { return new TemplateFragment(); } @SuppressWarnings("unused") public static final Parcelable.Creator<TemplateFragment.Creator> CREATOR = new Parcelable.Creator<TemplateFragment.Creator>() { @OverrIDe public TemplateFragment.Creator createFromParcel(Parcel in) { return new TemplateFragment.Creator(in); } @OverrIDe public TemplateFragment.Creator[] newArray(int size) { return new TemplateFragment.Creator[size]; } };}}
可以处理消息的初始容器活动如下所示:
Intent intent = new Intent(); intent.setClass(this.getActivity(), ContainerActivity.class); intent.putExtra(FragmentCreator.fragmentCreatorKey, new TemplateFragment.Creator()); startActivity(intent);
而片段“实例化其他片段”就像这样:
Bundle bundle = new Bundle(); bundle.putParcelable("argument", data); TemplateFragment.Creator creator = new TemplateFragment.Creator(bundle); creator.sendFragmentCreationMessage(getActivity());
Container Activity接收实例化请求:
public class ContainerActivity extends ActionBaractivity implements SetFragment, ShowDialog{private broadcastReceiver mFragmentCreationMessageReceiver = new broadcastReceiver(){ @OverrIDe public voID onReceive(Context context, Intent intent) { setFragment((FragmentCreator) intent.getParcelableExtra(FragmentCreator.fragmentCreatorKey)); }};private broadcastReceiver mFragmentDialogCreationMessageReceiver = new broadcastReceiver(){ @OverrIDe public voID onReceive(Context context, Intent intent) { showDialog((FragmentCreator) intent.getParcelableExtra(FragmentCreator.fragmentCreatorKey)); }};@OverrIDepublic voID onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); this.setContentVIEw(R.layout.activity_container); getActionbar().setdisplayHomeAsUpEnabled(true); if (saveInstanceState == null) { Fragment fragment = ((FragmentCreator) getIntent().getParcelableExtra( FragmentCreator.fragmentCreatorKey)).createFragment(); if (fragment != null) { replaceFragment(fragment); } } else { this.getActionbar() .setTitle( ((GetActionbarTitle) (this.getSupportFragmentManager() .findFragmentByID(R.ID.activity_container_container))) .getActionbarTitleID()); } getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() { public voID onBackStackChanged() { int backCount = getSupportFragmentManager().getBackStackEntryCount(); if (backCount == 0) { finish(); } } });}@OverrIDeprotected voID onResume(){ LocalbroadcastManager.getInstance(this).registerReceiver(mFragmentCreationMessageReceiver, new IntentFilter(FragmentCreator.fragmentCreationbroadcastMessage)); LocalbroadcastManager.getInstance(this).registerReceiver(mFragmentDialogCreationMessageReceiver, new IntentFilter(FragmentCreator.fragmentDialogCreationbroadcastMessage)); super.onResume();}@OverrIDeprotected voID onPause(){ super.onPause(); LocalbroadcastManager.getInstance(this).unregisterReceiver(mFragmentCreationMessageReceiver); LocalbroadcastManager.getInstance(this).unregisterReceiver( mFragmentDialogCreationMessageReceiver);}@OverrIDepublic voID setFragment(FragmentCreator fragmentCreator){ Fragment fragment = fragmentCreator.createFragment(); replaceFragment(fragment);}public voID replaceFragment(Fragment fragment){ if (fragment != null) { this.setTitle(((GetActionbarTitle) fragment).getActionbarTitleID()); getSupportFragmentManager().beginTransaction() .replace(R.ID.activity_container_container, fragment).addToBackStack(null).commit(); }}@OverrIDepublic voID showDialog(FragmentCreator fragmentCreator){ FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fragmentCreator.createFragment(); if (fragment instanceof DialogFragment) { DialogFragment df = (DialogFragment) fragment; df.show(fm, "dialog"); } else { Log.e(this.getClass().getSimplename(), "showDialog() called with non-dialog parameter!"); }}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item){ if (item.getItemID() == androID.R.ID.home) { this.onBackpressed(); } return super.onoptionsItemSelected(item);}}
我的问题是,这实际上是一个好主意,还是这是一个“过度工程”的可怕案例(为每个片段创建一个Factory并以本地广播的形式将其发送到Activity,而不是仅仅转换Activity最可能的持有者活动的界面和调用这样的功能)?
我的目标是这样,我可以使用相同的Activity来保存“分支”片段,这样我就不需要为每个菜单点制作一个.而不是仅仅重复使用相同的活动,并将所有逻辑划分为片段. (目前它不支持基于方向的布局组织,我看到了这个缺点 – 而且每个Fragment都需要保持静态创建者,这是额外的’样板代码’).
如果您知道为什么我不应该使用本地广播管理员的答案,我会很高兴听到回应.我觉得它非常整洁,但有可能只是过于简单的复杂化.
解决方法:
您可以使用Interface来实现Fragment可重用性的主要目标.您可以通过以下方式实现Activity-Fragment OR Fragment-Fragment之间的通信:
总结以上是内存溢出为你收集整理的android – 使用LocalBroadcastManager从Fragment到Activity进行通信全部内容,希望文章能够帮你解决android – 使用LocalBroadcastManager从Fragment到Activity进行通信所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)