In this class, a thread is created in which the game loop is implemented. And at some point I need to call DialogFragment
Dialog.show(getFragmentManager(),"GameOver"); But getFragmentManager() I can get only in the class inherited from AppCompatActivity . The same question was already in English stackoverflow, but I understood the answer. Here it is https://stackoverflow.com/questions/10268422/show-dialog-in-game-with-canvas
public class Menu extends AppCompatActivity implements GetDialog { private static final String TAG= Menu.class.getSimpleName(); MainGamePanel mainGamePanel; static float width,height; static boolean pauseGame = true; DialogFragment GameOverFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Display currentDisplay = getWindowManager().getDefaultDisplay(); width = currentDisplay.getWidth(); height = currentDisplay.getHeight(); // запрос на отключение строки заголовка /*requestWindowFeature(Window.FEATURE_NO_TITLE); // перевод приложения в полноэкранный режим getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);*/ mainGamePanel = new MainGamePanel(this); mainGamePanel.setInterface(this); setContentView(mainGamePanel); Log.d(TAG,"onCreate..."); } public void showDialog(){ GameOverFragment.show(getFragmentManager(), "dlg1"); } } .
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback { private SoundPool sounds; private int sExplosion; private int mStreamID; private AssetManager mAssetManager; private static final String TAG = MainThread.class.getSimpleName(); private MainThread thread; private Chronometer Treadchronometer; private mainCharacter Character, Character1, Character2, Character3, Character4; private Time time; Menu menu = new Menu(); public static long mLastTick; private float mSecondsElapsedTotal; float pSecondsElapsed; Bitmap touchpad; boolean Created, Destroy; Bitmap[] myArray = new Bitmap[11]; View mSurfaceView,mSurfaceHolder; GetDialog mGetDialog; public MainGamePanel(Context context) { super(context); getHolder().addCallback(this); mGetDialog.showDialog(); // делаем GamePanel focusable, чтобы она могла обрабатывать сообщения setFocusable(true); } public void setInterface(GetDialog name){ mGetDialog = name; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.d(TAG,"surfaceChanged"); } @Override public void surfaceCreated(SurfaceHolder holder) { mLastTick = System.nanoTime();// время от которого отсчитывается время на Update игровой ситуации Log.d(TAG,"surfaceCreated"); thread = new MainThread(getHolder(), this); thread.setRunning(true); thread.start(); Treadchronometer = new Chronometer(); Treadchronometer.setRunning(true); Treadchronometer.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { destroy(); } public synchronized void destroy(){ if(thread == null){ return; } // Добавить проверку на Null потока Treadchronometer boolean retry = true; thread.setRunning(false); Treadchronometer.setRunning(false); while (retry){ try{ Treadchronometer.join(); thread.join(); retry = false; } catch (InterruptedException ignored){ } } thread = null; Treadchronometer = null; Log.d(TAG,"surfaceDestroyed"); } }