17

when i start Camera intent, i noticed onActivityResult is called before onResume in fragment lifecycle.

I also noticed onActivityResult is called after onStart.

But here's the strange part: i have variable fileUri == "some image path". This variable is

  • NOT NULL in onStart.
  • NULL in onActivityResult
  • NOT NULL again in onResume

see logCat

12-03 14:39:42.418: D/Fragment1(29220): onStart fileUri: file:///mnt/sdcard/OPS_IMAGES/IMG_20121203_143933.jpg
12-03 14:39:42.463: W/PhoneWindow(29220): Previously focused view reported id 2131034140 during save, but can't be found during restore.
12-03 14:39:42.463: D/Fragment1(29220): onActivityResult fileUri is NULL!!!
12-03 14:39:42.468: D/Fragment1(29220): onResume fileUri: file:///mnt/sdcard/OPS_IMAGES/IMG_20121203_143933.jpg

Worst part is, this only happens 50% of the time. Another 50% onActivityResult can access fileUri value without problem...

How am i supposed to debug this?

NOTE: For the sake of simplicity, i didnt include code of my onResume, onStart, onActivityResult methods. They are just basic methods with variable check and log call. If needed, i will edit question and add these methods.

NOTE2: i am using google support library to support fragments on older API versions.

hendrix
  • 3,364
  • 8
  • 31
  • 46
  • Did you manage to solve that problem ? – Amit Dec 20 '12 at 06:14
  • 2
    Is it possible that they're different instances of the same `Fragment1` class? Add a `System.identityHashCode(this)` to the logs to print out a per-instance identifier – Xiao Aug 12 '15 at 06:15

2 Answers2

7

To answer your original question, here is the order of Lifecycle callbacks

12-09 16:38:41.800 10227-10227/org.Test I/Fragment: ## OnStart()
12-09 16:38:41.820 10227-10227/org.Test I/Fragment: ## OnActivityResult()
12-09 16:38:41.821 10227-10227/org.Test I/Fragment: ## OnResume()
Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
1

This is not an area I've been tinkering in or an issue that I've had, but if your variable is not visible for whatever reason... Have you tried using a different type of way to reference the value?

My suggestion would be to try using a SharedPreferences variable and saving the value there in the Editor and pulling it back out. This may not be the best solution but may be a work around for the moment.

I had the similar error message from logcat of, which is how I stumbled across your post.

"01-27 11:13:42.899: W/PhoneWindow(1591): Previously focused view reported id 16908862 during save, but can't be found during restore."

The way I was able to fix my issue was my "Blank Constructor" was not re-instansiateing my fragment. In the fragment I only have 1 variable being an 'ID' which is passed back to my activity when the user select an item with their selection. I simply re-instaniated my fragment by calling my normal constructor with the values passed in.

eg.

public UIDialogFragmentVolume() {
    this(ID);
}

public UIDialogFragmentVolume(int typeID) {
    ID = typeID;
}

I hope this helps you to determine and fix your issue.

toddles_fp
  • 7,451
  • 1
  • 16
  • 10