我不知道是什么导致了这种情况 - 我正在使用 Fragments 并在某些情况下覆盖后退按钮,但总的来说没有明显的原因说明为什么会发生这种情况。
我检查了 logcat,没有任何值得注意的信息显示为什么会发生这种情况 - 有谁知道可能是什么问题或要检查什么?
没有对 onDestroy 进行手动调用。
一旦我意识到存在问题,我就会尝试找出这种情况会在生命周期的早期发生,并且应用程序在安装时加载正常,然后如果我登录应用程序然后终止应用程序,我将无法加载它再次,我只得到一个白屏,如下所示。
Android list
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.year3.practise">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
主要 Activity
public class MainActivity extends AppCompatActivity {
private SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);
//initialise method
initFragment();
}
private void initFragment(){
Fragment fragment;
if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
fragment = new ProfileFragment();
}
else {
fragment = new QuestionPinFragment();
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.fragment_frame, fragment)
.commit();
}
}
}
最佳答案
您仅替换 else 子句中的 fragment 。
第二次打开应用程序时,首选项 IS_LOGGED_IN
为 true,并且不会显示 fragment 。
尝试使用以下代码:
private void initFragment(){
Fragment fragment;
if (pref.getBoolean(Constants.IS_LOGGED_IN,false)) {
fragment = new ProfileFragment();
} else {
fragment = new QuestionPinFragment();
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.fragment_frame, fragment)
.commit();
}
关于android - 应用程序首次被终止后启动时显示白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46595365/