0

The scenario is this, my Android app is placed in the background by the user tapping the Home button, answers an incoming call, taps a notification, etc. So, they look at Facebook, play Asphalt 8, anything that would cause the Android OS to find that it needs more memory and kill my Android app. The user decides that they want to look back at my app, tap the icon and BOINK!

When the app is started after it has been killed by the OS, the main Activity is passed a savedInstanceState that contains two keys: android:viewHierarchyState and android:support:fragments. This savedInstanceState is passed to the fragment and it crashes, stating that the fragment did not create a view:

Caused by: java.lang.IllegalStateException: Fragment com.foo.SomeFragment did not create a view.
  at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2200)
  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:404)
  at android.app.Activity.setContentView(Activity.java:2156)
  at com.foo.NavigationPage.onCreate(NavigationPage.java:774)
  at android.app.Activity.performCreate(Activity.java:5958)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
  at android.app.ActivityThread.access$800(ActivityThread.java:144) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
....

So, the OS is using data it wrote the the savedInstanceState when it killed the app but for some reason, it is corrupt when the app uses it to create itself on startup.

I have tried doing this within the NavigationPage activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null)
}

Which works for mobile app startup but if your mobile app goes into the background and you tap the icon, you expect it go come back to where you left off. Passing null causing the app to go through all its screen building/loading process which is time-consuming and not the user experience we would want.

Any thoughts on what could be happening or how I could avoid this situation?

Perry Hoekstra
  • 2,687
  • 3
  • 33
  • 52

1 Answers1

0

This has been answered here: Fragment did not create a view

it literally says:

You need to reverse this:

setContentView(R.layout.main_activity); 
super.onCreate(savedInstanceState). 

into this:

super.onCreate(savedInstanceState). 
setContentView(R.layout.main_activity);

in your activity's onCreate.

Community
  • 1
  • 1
Jhoon
  • 59
  • 6
  • Sorry, I should have mentioned that I looked at that question and in my NavigationPage, the code is: super.onCreate(savedInstanceState); setContentView(R.layout.navigation); – Perry Hoekstra Aug 25 '15 at 21:37
  • I also looked at this posting with no joy: http://stackoverflow.com/questions/19874882/android-view-inflateexception-binary-xml-file-error-inflating-class-fragment – Perry Hoekstra Aug 25 '15 at 21:39
  • what about the `onSaveInstanceState` method? maybe it's also saving a null? – Jhoon Aug 25 '15 at 21:48
  • No, I dumped the Bundle through debug and see: 08-25 17:15:38.205 22849-22849/? D/com.foo.NavigationPage﹕ android:viewHierarchyState is a key in the bundle 08-25 17:15:38.205 22849-22849/? D/com.foo.NavigationPage﹕ key [0], value [android.view.AbsSavedState$1@483fabc] 08-25 17:15:38.205 22849-22849/? D/com.foo..NavigationPage﹕ key [1], value [android.view.AbsSavedState$1@483fabc] 08-25 17:15:38.205 22849-22849/? D/com.foo..NavigationPage﹕ key [2], value [android.view.AbsSavedState$1@483fabc] ... – Perry Hoekstra Aug 25 '15 at 22:17