Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is fragment's onActivityResult called in respect to fragments lifecycle?

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.

like image 223
hendrix Avatar asked Dec 03 '12 13:12

hendrix


People also ask

How can we call fragment Onactivityresult from activity?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment. @StErMi Make sure you call startActivityForResult() and not getActivity(). startActivityForResult() from your fragment.

What is the lifecycle of a fragment?

Each Fragment instance has its own lifecycle. When a user navigates and interacts with your app, your fragments transition through various states in their lifecycle as they are added, removed, and enter or exit the screen.

What does the onCreateView () method return if a fragment doesn't have any?

Returns The fragment's root view, or null if it has no layout.


2 Answers

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()
like image 192
Roman Nazarevych Avatar answered Sep 19 '22 21:09

Roman Nazarevych


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.

like image 36
toddles_fp Avatar answered Sep 22 '22 21:09

toddles_fp