如何在新流程中打开Android Fragment? [英] How can I open an Android Fragment in a new process?

查看:83
本文介绍了如何在新流程中打开Android Fragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用的应用程序使用导航抽屉,每个选项卡在活动中打开一个新片段(替换旧片段).

The application I'm currently working on uses a Navigation Drawer, and each tab opens up a new fragment in the activity (replacing the old one).

这些片段之一是Unity3D场景.基本上,我所做的是:

One of these Fragments is a Unity3D scene. Basically, what I did was:

  1. 将Unity项目导出为Android应用
  2. 打开它提供的活动(这是UnityPlayerNativeActivity)
  3. 将此UnityPlayerNativeActivity转换为片段,如下面的代码所示.我还更改了清单中的一些小事情.

  1. export the Unity project as an Android app
  2. open up the Activity that it gave (which was UnityPlayerNativeActivity)
  3. convert this UnityPlayerNativeActivity to 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);
}
}

这里的问题是mUnityPlayer.quit()函数.如此处所述,mUnityPlayer.quit( )完全杀死了正在运行的进程.所以基本上发生的是,当我切换片段(即NavigationDrawer中的切换选项卡)时,整个应用程序都会退出,因为这会调用UnityPlayerNativeFragment的onDestroy.

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

我不想要这个.理想情况下,用户将切换到NavigationDrawer中的另一个选项卡,并且当他/她返回Unity场景的选项卡时,它正是他/她离开它的位置.就像他们使用Android多任务处理来更改应用程序并返回一样-没什么变化.

I do NOT want this. Ideally, the user would switch to another tab in the NavigationDrawer, and when he/she comes back to the tab of the Unity scene, it is exactly where he/she left it. As though they used the Android multi-tasking to change apps and return - nothing changes.

如果这是一个Activity,则可以通过在Manifest文件中添加android:process=":UnityKillsMe"行,以与主应用程序不同的方式打开它,但这对于片段来说是不可能的.所以,我的问题:

If this were an Activity, it would be possible to open it in a different process than the main app by adding the android:process=":UnityKillsMe" line in the Manifest file, but this isn't possible for a fragment. So, my questions:

  1. 如何在新的过程中打开UnityPlayerNativeFragment,以使.quit()函数不会杀死我的整个应用程序,并在用户离开选项卡然后返回至该选项卡时重新启动该片段?
  2. 即使我可以实现Q1,也仍然存在状态问题.退出然后重新加载Unity场景将会从默认视图开始重新创建它,并丢失其拥有的所有属性(例如,假设这是一个游戏,那么所有数据都会丢失).所以问题2是:如何在不更改场景的情况下切换到同一应用程序中的另一个选项卡,然后返回到Unity选项卡? (本质上是,使其始终在后台运行而无需退出).

这是我(从MainActivity)切换片段的方式:

Here's how I'm switching fragments (from the 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();
        }
    }

推荐答案

感谢您的代码. 我正在查找代码,如何将统一活动更改为一个片段.

thanks for your code . I'm finding the code how to change unity activity to a fragment.

对于您的问题,我在想为什么您需要调用mUnityPlayer.quit();?在Destory()方法上. onDestory方法表示片段完成.不是活动.

For your question , I'm thinking why you need to call mUnityPlayer.quit(); on Destory() method . the onDestory method mean the fragment finish . not the activity .

我认为删除quit方法.在具有MainActivity的片段之间添加一个侦听器,当MainActivity onDestroy时,调用fragment方法来调用unity quit.

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

这篇关于如何在新流程中打开Android Fragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆