How to open an Android fragment in a new process?

The application in which I am currently working uses a navigation box, and each tab opens a new fragment in action (replaces the old one).

One of these snippets is the Unity3D script. I basically did this:

  • Export your Unity project as an Android app.
  • open the action it gave (this is UnityPlayerNativeActivity)
  • convert this UnityPlayerNativeActivity into a fragment, as shown in the code below. I also changed some minor things in the manifest.

    import com.unity3d.player.*; import android.app.Fragment; import android.app.NativeActivity; import android.content.res.Configuration; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; public class UnityPlayerNativeFragment extends Fragment { protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code private static final String ARG_SECTION_NUMBER = "section_number"; public static UnityPlayerNativeFragment newInstance(int sectionNumber) { UnityPlayerNativeFragment fragment = new UnityPlayerNativeFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } // Setup activity layout @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getActivity().getWindow().takeSurface(null); getActivity().setTheme(android.R.style.Theme_NoTitleBar_Fullscreen); getActivity().getWindow().setFormat(PixelFormat.RGB_565); mUnityPlayer = new UnityPlayer(getActivity()); if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true)) getActivity().getWindow().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1); boolean trueColor8888 = false; mUnityPlayer.init(glesMode, trueColor8888); mUnityPlayer.windowFocusChanged(true); mUnityPlayer.setX((float)-5); View playerView = mUnityPlayer.getView(); return playerView; } // Quit Unity @Override public void onDestroy () { mUnityPlayer.quit(); super.onDestroy(); } // Pause Unity @Override public void onPause() { super.onPause(); mUnityPlayer.pause(); } // Resume Unity @Override public void onResume() { super.onResume(); mUnityPlayer.resume(); } // This ensures the layout will be correct. @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mUnityPlayer.configurationChanged(newConfig); } public void onWindowFocusChanged(boolean hasFocus) { mUnityPlayer.windowFocusChanged(hasFocus); } } 

The problem here is in the mUnityPlayer.quit () function. As stated here , mUnityPlayer.quit () completely kills the process in which it is running. So basically what happens is that the whole application exits when I switch fragments (i.e., I switch tabs in NavigationDrawer) because it calls onDestroy UnityPlayerNativeFragment.

I do not want it. Ideally, the user will switch to another tab in the NavigationDrawer, and when he returns to the Unity scene tab, this is exactly where he left it. As if they used Android multitasking to change applications and return - nothing changes.

If it was an Activity, you could open it in a different process than the main application by adding the line android:process=":UnityKillsMe" to the manifest file, but this is not possible for the fragment. So my questions are:

  • How can I open UnityPlayerNativeFragment in a new process so that the .quit () function does not kill my entire application and that I can restart the fragment when the user leaves the tab and then returns to it?
  • Even if I can reach Q1, the state problem remains. Completing and then reloading the Unity scene will recreate it again, starting with the default view and losing any properties that it had (for example, imagine that it was a game, then all data will be lost). So, question 2: how to switch to another tab in the same application, and then return to the Unity tab without changing the scene? (Essentially supporting it in the background without leaving it at all).

Here's how I switch fragments (from MainActivity):

 @Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getFragmentManager(); if (position==3) { fragmentManager.beginTransaction() .replace(R.id.container, DirectoryFragment.newInstance(position + 1)) .commit(); } else if (position == 0) { upnf = UnityPlayerNativeFragment.newInstance(position + 1); fragmentManager.beginTransaction() .replace(R.id.container, upnf, "UnityPlayerNativeFragment") .commit(); } else if (position == 2){ fragmentManager.beginTransaction() .replace(R.id.container, WeatherFragment.newInstance(position + 1)) .commit(); } else if (position == 4) { fragmentManager.beginTransaction() .replace(R.id.container, SettingsFragment.newInstance(position + 1)) .commit(); //Log.d("worked pos=4","worked pos=4"); } else{ fragmentManager.beginTransaction() .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) .commit(); } } 
+5
source share
1 answer

Thanks for your code. I find code on how to change unity activity into fragment.

For your question, I think why you need to call mUnityPlayer.quit (); according to the method of Destory (). the onDestory method means the completion of the fragment. not activity.

I think remove the quit method. add a listener between the fragment with MainActivity, when MainActivity onDestroy, call the fragment method to invoke unity.

0
source

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


All Articles