Application showing white screen on start-up after application is killed for the first time

Viewed 345

I have no idea what is causing this - I am using Fragments and overriding the back button on a few occasions but overall nothing obvious as to why this is happening.

I have checked the logcat and nothing of note showing as to why this is happening - does anyone have any idea what might be the issue or what to check?

There are no manual calls being made to onDestroy.

Once I realised there was a problem, I then tried to find out how early in the lifecycle this would happen and the app loads fine on installation and then if I log into the app and then kill the app, I cannot load it again, I just get a white screen as shown below.

Android Manifest

<?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>

MainActivity

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();
    }

   }

}

enter image description here

1 Answers

You are replacing the Fragments only in the else clause.
The second time that you open the application the preference IS_LOGGED_IN is true and the fragment is not shown.

Try with this code:

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();        
}
Related