OnCreate 不调用

我有2个活动: 第一个活动用户点击一个按钮,启动第二个活动。第二个活动完成所有的工作。

我按照下面的方式启动第2个 Activity,它位于 onClickListener 内部类中,并且我尝试用(FirstActivity.this,Simple)显式地调用它。类) ,但同样的事情发生了。

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

在模拟器上,我看到屏幕像调用第二个活动一样移动,但是我得到的只是一个黑色的屏幕,但是没有从我的布局中加载任何东西。我查看了 logcat,确实看到了一些活页夹线程失败的消息。这是我第二个活动中的 onCreate 函数,但是我没有从屏幕或 logcat 中得到任何结果,显示 Log 函数已被调用:

    public void onCreate(Bundle savedState)
{
Log.d("SimpleActivity","OnCreate Started");


super.onCreate(savedState);
setContentView(R.layout.simple);


Log.d("SimpleActivity","OnCreate Ended");
}

注意: 在上面的代码中,我使用 super.onCreate (savedState)调用了 OnCreate ()中的基本构造函数。

34913 次浏览

you should @Override onCreate and add super.onCreate() in it

@Override
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);


Log.d("SimpleActivity","OnCreate Started");
setContentView(R.layout.simple);
Log.d("SimpleActivity","OnCreate Ended");
}

You need to call the super.onCreate(savedState) method. Take a look at Activity doc.

 public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
}

It's possible that onCreate doesn't get called, if the activity has never been destroyed, if for some reason an activity hangs around, next time its instantiated it is not recreated but resumed instead...

At least that's what im Dealing with right now in my code... Life cycle of Activities seem a good logical explanation.. However 99% of time I do rely on onCreate being called when startingActivity and it doesn't fail me....

Edit: And of course its because I wasn't calling finish() when exiting the activity. Doh.

This is not related to this certain issue, but also this can happen when activity is not declared in manifest file)

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!

Be careful that if your method belongs to AppCompatActivity or Activity .

It is up to what you implemented to your Class

If you want to add lifecycle or any override methods, I recommend you to press CTRL+O or do Code > Override methodsand there you can see where the method belongs

Screenshot for illustrative purposes - Android Studio's "Methods to Override" dialog

remove android:launchMode="singleTask" from manifest

my case

(1) mainActivity -> (2) open Adaptor - startActivity -> (3) mainActivity onCreate() doesn't get to triggered.

I resolved this by adding finish();. in mainActivity.

follow the below steps to check your application.

1.did you override the right method? if not overriding the below method, this method will be triggered, when you startActivity.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

2.Make sure that you registered the activity in manifest.xml

2.1 is your activity has android:launchMode="singleInstance" ? (if your application doesn't need to be singleinstance, consider to remove. but my case I need singleinstance. hence i moved to the next step)

  1. use finish()

     public void openSearch(View view) {
    Intent intent = new Intent(this, BookInfoActivity.class);
    intent.putExtra(...);
    startActivity(intent);
    finish(); // add like this.}
    

why do we need to use "finish()"?

Screen A -> click button on A -> Screen B -> click button on B -> screen A with some new data that you get from Screen B

if you don't call finish() method(in A button) , that means the A is still in your background even you are seeing the screen B.

hence, when you trigger startActivity on screen B, it just simply shows the running A screen.

however if you use finish() method (in A button), when you go to B Screen, it destroys the A screen, so when you go back to A screen by clicking B method( 'StartActivity') it creates A screen and trigger onCreate() Method .