Preload all fragments?

Initial situation:

Hi, I am currently working on updating an existing application and have problems with one specific action. It contains 7 tabs, and on these tabs there are several image buttons and text views for different methods.

In the original version of the application, I used tabhost and basically had all the content needed in one xml file and one java file, but it was not possible to scroll the tabcontent sideways to change the tabs, so I created the whole thing again with fragments.

Problem:

In ActionBar, I have a Filter button that should hide certain text objects and image buttons from all tabs, checking the flag and then calling the methods in all 7 fragment classes, which then set the views to their rootView in GONE and if switched again, to VISIBLE.

Problem:

This ActionBar button works fine if users first view all the tabs, but if you click on the cold start of the application, this will cause the application to crash with NullPointer exceptions. I suspect this is because not all fragments are loaded yet.

Question:

Should I preload all the fragments at the beginning of the action? If so, how do I do this and in which file will this be done? Is there another, maybe better, way to make sure that I can manipulate all the tabs / fragments from the ActionBar, even if I don’t know which fragments were opened before? Should I brake all of these methods in one global? What file will it go to?

Currently, it only works if you first view all the tabs and then change the filter as often as I want.

Code:

This is called in the activity class:

public void toggleintroonly() { MenuItem toggle = menu.findItem(R.id.mtoggle); if (visible == 1) { Fragments1.toggleintroonly(); Fragments2.toggleintroonly(); Fragments3.toggleintroonly(); Fragments4.toggleintroonly(); Fragments5.toggleintroonly(); Fragments6.toggleintroonly(); Fragments7.toggleintroonly(); toggle.setTitle(getString(R.string.menu4toggled)); toggle.setIcon(R.drawable.ic_action_showall); visible = 0; } else { Fragments1.showall(); Fragments2.showall(); Fragments3.showall(); Fragments4.showall(); Fragments5.showall(); Fragments6.showall(); Fragments7.showall(); toggle.setTitle(getString(R.string.menu4)); toggle.setIcon(R.drawable.ic_action_filter); visible = 1; } } 

This is an example from Fragments1.toggleintroonly ():

 public static void toggleintroonly() { rootView.findViewById(R.id.tv1x02).setVisibility(View.GONE); rootView.findViewById(R.id.bplay1x02).setVisibility(View.GONE); rootView.findViewById(R.id.bshare1x02).setVisibility(View.GONE); rootView.findViewById(R.id.binfo1x02).setVisibility(View.GONE); rootView.findViewById(R.id.bring1x02).setVisibility(View.GONE); } 

The following is the error when clicking the Filter button from the ActionBar immediately after starting the Activity:

 10-27 16:43:34.923: E/MediaPlayer(16946): mOnCompletionListener is null. Failed to send MEDIA_PLAYBACK_COMPLETE message. 10-27 16:43:49.433: D/AndroidRuntime(16946): Shutting down VM 10-27 16:43:49.433: W/dalvikvm(16946): threadid=1: thread exiting with uncaught exception (group=0x40c511f8) 10-27 16:43:49.448: E/AndroidRuntime(16946): FATAL EXCEPTION: main 10-27 16:43:49.448: E/AndroidRuntime(16946): java.lang.NullPointerException 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.cheftony.psychsoundboard.Fragments3.toggleintroonly(Fragments3.java:225) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.cheftony.psychsoundboard.NicknamesNew.toggleintroonly(NicknamesNew.java:121) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.cheftony.psychsoundboard.NicknamesNew.onOptionsItemSelected(NicknamesNew.java:90) 10-27 16:43:49.448: E/AndroidRuntime(16946): at android.app.Activity.onMenuItemSelected(Activity.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at android.view.View.performClick(View.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at android.view.View$PerformClick.run(View.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at android.os.Handler.handleCallback(Handler.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at android.os.Handler.dispatchMessage(Handler.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at android.os.Looper.loop(Looper.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at android.app.ActivityThread.main(ActivityThread.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at java.lang.reflect.Method.invokeNative(Native Method) 10-27 16:43:49.448: E/AndroidRuntime(16946): at java.lang.reflect.Method.invoke(Method.java:511) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 10-27 16:43:49.448: E/AndroidRuntime(16946): at dalvik.system.NativeStart.main(Native Method) 

The error seems to be caused in the Fragments3 class, because when the Activity starts, Fragments1 is active, as well as its preloader with direct proximity.

+4
source share
1 answer

A few tips:

  • If you work with 7 tabs, you can use ViewPager . This will help speed up the user. :)

  • Make sure your Fragment does not intend to create an Activity until onActivityCreated() is called. This may be the reason that you are getting a NullPointerException .

  • Your Fragment should act as independent user interface components. If your Fragment interacts with each other, you can rethink your design, as this probably complicates the situation.

  • If your Fragment does not create a crazy long time to create / render, I am not sure if Fragment must be preloaded. With 7 tabs, the user may not use all Fragment s, so loading all of them can be wasteful. Preloading some favorites also does not make sense, since you can never know which tab the user selects. It seems to me that the best solution would be to simply initialize a new Fragment and fill it with a tab when the user clicks on the tab.

+2
source

Source: https://habr.com/ru/post/1442423/


All Articles