【问题标题】:onCreate() runs for the first timeonCreate() 首次运行
【发布时间】:2015-09-09 11:53:35
【问题描述】:

是否有代码可以用来测试 onCreate() 是否第一次被调用?

例如

if (condition) {
 code that runs if the app runs for the first time 
}else{
 code that runs if activity is started because of configuration changes, orientation changes...
} 

【问题讨论】:

  • 在顶部声明一个变量 i。然后为每个 oncreate 方法调用递增 i 。如果 1 == 1,那么......。

标签: android oncreate


【解决方案1】:

这就是我解决问题的方法,但不知道这是否是正确/唯一的方法

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(savedInstanceState == null){
        Log.d("SEBA", "onCreate in if");
    }else {
        Log.d("SEBA", "onCreate in else");
    }}

【讨论】:

  • 这可能是正确的方法,但你想做什么?除了记录它;)
猜你喜欢
  • 2014-11-25
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 2010-10-30
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多