Android: Use the newInstance pattern for EmulationFragment.

This commit is contained in:
Mike 2017-11-01 21:59:30 -07:00
parent e29cd19f73
commit 987d24fe87
4 changed files with 30 additions and 13 deletions

View File

@ -42,6 +42,7 @@ import org.dolphinemu.dolphinemu.ui.platform.Platform;
import org.dolphinemu.dolphinemu.utils.Animations; import org.dolphinemu.dolphinemu.utils.Animations;
import org.dolphinemu.dolphinemu.utils.Java_GCAdapter; import org.dolphinemu.dolphinemu.utils.Java_GCAdapter;
import org.dolphinemu.dolphinemu.utils.Java_WiimoteAdapter; import org.dolphinemu.dolphinemu.utils.Java_WiimoteAdapter;
import org.dolphinemu.dolphinemu.utils.Log;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.util.List; import java.util.List;
@ -226,8 +227,17 @@ public final class EmulationActivity extends AppCompatActivity
setContentView(R.layout.activity_emulation); setContentView(R.layout.activity_emulation);
mImageView = (ImageView) findViewById(R.id.image_screenshot); mImageView = (ImageView) findViewById(R.id.image_screenshot);
// Find or create the EmulationFragment
mEmulationFragment = (EmulationFragment) getSupportFragmentManager() mEmulationFragment = (EmulationFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_emulation); .findFragmentById(R.id.frame_emulation_fragment);
if (mEmulationFragment == null)
{
mEmulationFragment = EmulationFragment.newInstance(path);
getSupportFragmentManager().beginTransaction()
.add(R.id.frame_emulation_fragment, mEmulationFragment)
.commit();
}
if (savedInstanceState == null) if (savedInstanceState == null)
{ {
@ -266,7 +276,6 @@ public final class EmulationActivity extends AppCompatActivity
} }
}); });
mEmulationFragment.setGamePath(path);
mEmulationFragment.startEmulation(); mEmulationFragment.startEmulation();
} }
else else

View File

@ -21,6 +21,8 @@ import org.dolphinemu.dolphinemu.utils.Log;
public final class EmulationFragment extends Fragment implements SurfaceHolder.Callback public final class EmulationFragment extends Fragment implements SurfaceHolder.Callback
{ {
private static final String KEY_GAMEPATH = "gamepath";
private SharedPreferences mPreferences; private SharedPreferences mPreferences;
private Surface mSurface; private Surface mSurface;
@ -32,6 +34,17 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
private String mGamePath; private String mGamePath;
private final EmulationState mEmulationState = new EmulationState(); private final EmulationState mEmulationState = new EmulationState();
public static EmulationFragment newInstance(String gamePath)
{
Bundle args = new Bundle();
args.putString(KEY_GAMEPATH, gamePath);
EmulationFragment fragment = new EmulationFragment();
fragment.setArguments(args);
return fragment;
}
@Override @Override
public void onAttach(Context context) public void onAttach(Context context)
{ {
@ -59,6 +72,8 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
setRetainInstance(true); setRetainInstance(true);
mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
mGamePath = getArguments().getString(KEY_GAMEPATH);
} }
/** /**
@ -114,11 +129,6 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
super.onDetach(); super.onDetach();
} }
public void setGamePath(String gamePath)
{
mGamePath = gamePath;
}
public void toggleInputOverlayVisibility() public void toggleInputOverlayVisibility()
{ {
SharedPreferences.Editor editor = mPreferences.edit(); SharedPreferences.Editor editor = mPreferences.edit();

View File

@ -5,9 +5,8 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame_content"> android:id="@+id/frame_content">
<fragment <FrameLayout
android:id="@+id/fragment_emulation" android:id="@+id/frame_emulation_fragment"
android:name="org.dolphinemu.dolphinemu.fragments.EmulationFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>

View File

@ -3,9 +3,8 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/frame_content"> android:id="@+id/frame_content">
<fragment <FrameLayout
android:id="@+id/fragment_emulation" android:id="@+id/frame_emulation_fragment"
android:name="org.dolphinemu.dolphinemu.fragments.EmulationFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>