Tag: android lifecycle

Android – 在Fragment类中保存成员字段的值

每次显示Fragment时,我都试图在Fragment类中使用boolean做一些事情。 例 我的应用程序启动,打开FirstFragment并且第一次boolean始终为true ,然后我有一个if子句来检查它的值: if (FirstTime) { FirstTime = false; } else { // Other stuff here, cause it’s not true. } 然后,在第一次,当FirstTime为true ,我做的事情就像去另一个Fragment 。 当我返回Fragment1和onCreate() ,我也会这样做。 它总是true ,似乎它是令人耳目一新的东西。 然后我认为这可能是Fragment一个问题,每次我按下Fragment1 ,它都会重启或者什么的。 然后,我在MainActivity添加了一个getter和setter: public Boolean getFirstTime() { return FirstTime; } public void setFirstTime(Boolean FirstTime) { this.FirstTime = FirstTime; } 从开始以来,它是真的,然后,我从Fragment1更改了我的代码: if (((MainActivity) getActivity()).getFirstTime()) ((MainActivity) getActivity()).setFirstTime(false); } […]

Android – 活动构造函数

我注意到使用快捷键Alt + Insert并选择构建器,它尝试使用每个私有属性(例如cManager和mTextView )创建构造函数。 我记得我已经创建了没有它的项目并且工作正常。 在运行应用程序之后,在完整的logcat和Activity构造函数之后发生错误: 具有私有财产的构造函数: private ConnectivityManager cManager; private TextView mTextView; public SplashScreenActivity() { this.cManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); this.mTextView = (TextView)this.findViewById(R.id.mProgressText); } logcat的: 02-20 18:20:56.155 1591-1591/com.universo91.towersrock I/art﹕ Not late-enabling -Xcheck:jni (already on) 02-20 18:20:58.355 1591-1591/com.universo91.towersrock D/AndroidRuntime﹕ Shutting down VM ——— beginning of crash 02-20 18:20:58.361 1591-1591/com.universo91.towersrock E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.universo91.towersrock, PID: […]