Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreate not called

I have 2 Activities : First activity user clicks on a button which launches the 2nd activity. The 2nd Activity does all the work.

I launch the 2nd Activity as follows which is inside a onClickListener Inner Class and I have tried explicitly calling it with (FirstActivity.this,Simple.Class) but same thing happens.

    Intent test = new Intent(arg0.getContext(),Simple.class);     startActivity(test); 

On the emulator, I see the screen move over like its calling the 2nd activity but all I get is a black screen but nothing is loaded from my layout. I looked at logcat and I do see some binder thread failed messages. This is the onCreate function from my 2nd activity but I do not get any results from either the screen or logcat showing me that the Log functions were called:

    public void onCreate(Bundle savedState)     {        Log.d("SimpleActivity","OnCreate Started");         super.onCreate(savedState);    setContentView(R.layout.simple);         Log.d("SimpleActivity","OnCreate Ended");     } 

Note : I have called the base constructor in OnCreate() with super.onCreate(savedState) in my code above.

like image 312
barakisbrown Avatar asked Dec 15 '11 02:12

barakisbrown


People also ask

Is onCreate always called?

The Android documentation says the method onCreate is called only when is starting or when is destroyed. Why is this happening? Thanks!! Regars.

Is onCreate only called once?

@OnCreate is only for initial creation, and thus should only be called once. If you have any processing you wish to complete multiple times you should put it elsewhere, perhaps in the @OnResume method.

How many times is onCreate called?

OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.

What does onCreate mean?

onCreate is used to start an activity. super is used to call the parent class constructor. setContentView is used to set the xml.


1 Answers

What happened to me was I was overriding the wrong onCreate method. I was overriding public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) when I really needed to override protected void onCreate(@Nullable Bundle savedInstanceState). Maybe this might help someone!

like image 66
Rock Lee Avatar answered Sep 21 '22 04:09

Rock Lee