0

Hello I have a Main activity and this mainactivity has a mainview. This mainview is a surfaceview. For rendering the logic to draw on the surface view teh mainview has a mainthread.

So now I have a Problem which I hadn't before: The first App I make in landscape so I set all activities' orientation to landscape.

When the App now gets through onDestroy the Mainview gets null.

And I dont get why!!!

Here the onCretae:

MainView mView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(mView==null)
        Log.e("","mainview==nullllll");
    if(savedInstanceState==null)
    {
        mView=new MainView(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(mView);
    }
}

and here the onDestroy:

@Override
public void onDestroy()
{
    super.onDestroy();
    if(mView==null)
        Log.e("","mainview==nullllll");

}

What can I do to prevent my Mainview to be nullified? having two mainviews so to make sure it doesnt get garbage collected and refill the nullified one?

I dont want to set up the thread and the view every time.

I wonder why the Activity gets Destroyed on the other hand. I'm just locking the screen and in my other apps(where the Screen orientation is locked to portrait) doesn't behave like that it doesnt get into the destroy method if it is not really destroyed. It should get into the stopped state and wait there to be resumed again and not getting destroyed!

Please help me guys!

Ilja KO
  • 1,272
  • 12
  • 26
  • save activity state before `onDestroy()` is called. – karan Jul 29 '15 at 11:46
  • can you elaborate how I should do this? by reading out the bundle savedinstancestate? – Ilja KO Jul 29 '15 at 11:49
  • check accepted answer over here http://stackoverflow.com/questions/151777/saving-activity-state-in-android – karan Jul 29 '15 at 11:50
  • You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this: – karan Jul 29 '15 at 11:51
  • Its not so easy in my case I will have a huge amount of values and objects to be saved. Also and this is the crucial point: a lot of bitmaps that I want to hold in memory so they won't be garbage collected and taking every time the user gets back to the game seconds of loading – Ilja KO Jul 29 '15 at 11:56
  • also the main thread has a reference to the mainview and inside his loop the mainview is not null so it still exists! – Ilja KO Jul 29 '15 at 11:57
  • i can only think of this as possible solution for now, or you need to save them somewhere, sharedpreferences will do but thats costly – karan Jul 29 '15 at 11:58
  • If you have a huge amount of vaues you can save your MainView in your custom Application Class...but some people won't agree with that...another way can be to let your object implement Parcelable so you can easily save in to the SaveInstanceState's Bundle – Stefano Vuerich Jul 29 '15 at 12:09
  • When you rotate the screen the complete activity gets destroyed and re-created. Why not use a fragment and display your main view in that. You can use setRetainInstance(true) to keep the fragment from being destroyed. – Arno van Lieshout Jul 29 '15 at 12:10
  • isn'T the fragment bound to the activity and gets destroyed too when the activty is destroyed? – Ilja KO Jul 29 '15 at 12:15
  • And I dont rotate the screen its locked into landscape mode. I just lock the screen but that obviously causes an orientation change(but not when pressing the home button and leave the activity in that way) – Ilja KO Jul 29 '15 at 12:16

1 Answers1

0

I've found the Solution:

I just need to add these Lines into the Manifest(>API13 solution)

android:configChanges="orientation|screenSize"

So the Activity won't be destroyed...

Ilja KO
  • 1,272
  • 12
  • 26