dimanche 3 mai 2015

Removing layout exits my application

I have this code for initial/default layout

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
      // Retrieve the shared preferences
    mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);

    glView = new GLSurfaceView(this);
    glView.setRenderer(this);
    glView.setZOrderMediaOverlay(false);
    glView.setId(123); // set id 

    layout = new RelativeLayout(this);
    // create a fake view with zero size and place it to center of RelativeLayout
    fakeView = new View(this); 
    fakeView.setId(24736); 
    fakeparams = new RelativeLayout.LayoutParams(0, 0);
    fakeparams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.ABOVE, fakeView.getId()); // set position is above fakeView
    layout.addView(glView, params);

    setContentView(layout);

Say we are in main menu and in some menu when user click it, i'll inflate new layout which I prepared in res/layout folder

    myStoryView = getLayoutInflater().inflate(R.layout.story2, layout, false);
    layout.addView(myStoryView);

This goes well, I get the result, new layout appear as it should. I then plan to return back to main menu. So I call this when user hit back button:

    layout.removeView(myStoryView);

But what happened is I lose all my layout. It exits entire app, though in my galaxy tab when I hold center button, I can see my app is there, maybe it's not entirely closed. I only have no layout to display.

Aucun commentaire:

Enregistrer un commentaire