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"); } } 

    1 answer 1

    1. Create an interface with the void showDialog() method
    2. Implement it in actviti.
    3. Create a field with the type of interface in the desired class. Create a setter to it.
    4. In actviti, when creating an instance of your class, call the setter method and pass the actviti to it.
    5. In the class at the right time, call the method from item 1 of the variable and item 3 - this is how the method from item 1 implemented in actviti will be called. And there you have access to the FragmentManager
    • So ... Point one did, created a method in it. Point 2 did, in the course of course I naturally created an empty method public void showDialog () {}. The third point. Field with interface type? How is it declared, created? Point 4. those. Assign this to the one created in paragraph 3 of the activation. How? (I have a very weak base in OOP and Java, but I want to finish the game before I start reading books) - Turalllb
    • You need to declare a variable at the class level as follows: ИмяИнтерфейса mInterface; . Create a setter for it public void setInterface(ИмяИнтерфейса name){mInterface = name; } public void setInterface(ИмяИнтерфейса name){mInterface = name; } Then somewhere - переменнаяВашегоКласса.setInterface(this); - Yuriy SPb
    • one
      Thank you. 12 hours behind computers made themselves felt. I am stupidly confused in my logs, the activation does not stop. The log output: D / ContentValues: Stopping menu, I understand it just the value of a variable? and after exiting the activation, already D / Menu: Stopping menu. And I also declared a DialogFragment, but did not initialize it in the onCreate () method. Now everything works, thanks. - Turalllb
    • one
      @Turalllb, if your code hasn't changed much, then you have null, because You are trying to search View before uploading markup using the method setContentView - Jurijspb
    • one
      You are very on time, otherwise I have already begun to write a new question) I will continue to pay attention to this. And the truth is that the loading of markup went to the very bottom while I modified the rest of the code. It's hard to code after a day on duty, but I want to create) Thank you! - Turalllb