Situation
My activity waits on an Async operation and after it hears back from async operation, it needs to pass information to 2 fragments inside it.
Requirement
1. Both fragments need their onCreateView calls to be done for them to have their layouts loaded,
2. They need for themselves to be attached to their activity so that getActivity() works.
I wrote a setData() method in both the fragments and am looking for the "correct" place in the activity's lifecycle to invoke them.
onCreate() of the activity does not work, onStart() of the activity does not work and onStart() of the fragment does not work.
Nothing works, what am I missing here?
The official documentation for the Fragment lifecycle explains this clearly - please refer to it and then ask follow-up questions if something is unclear.
This Image will be helpful to understand both life cycles together.
As many people complaints and it is somewhat valid argument that this life cycle is too complicated, in Google I/O 2018,They have suggested to use Architecture component Framework. Please check this Docs
when you are at Activity2---->backpress--->Fragment2(Activity1)---means Activity1 again attach from fragment2 so on OnAactivityCreated() method Activity1 is completely loaded ....so at that we can call setData() method of your Activity1...
onAttachFragment()-activity is called before onCreate()-activity and after onAttach()-fragment
Call onDestroy on onStop of your fragment. This should call onCreate when the fragment is launched.
Let me know if works as an ideal solution for your problem.
Related
I am just asking for knowledge , can we call Interface from one activity to another activity
If it possible then can anyone share code with me?
You can't
Why?
Activity object is created by system. We all just call startActivity() & work in its lifecycle methods like onCreate(), onStart() etc.
If you really need it you can use fragment.
Android activity and fragment lifecycles have many stages (onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy(), etc). I have been coding in Android Studio for a few months now and there're many times where I haven't used all of lifecycle methods.
My question is that do you need to use all of the lifecycle methods of a fragment or an activity to write a good code? Will it cause crashes otherwise?
Nope. You can override those methods to add more functionality to your app but those methods already have their own function and will run whether you override it or not.
You could read more on the Android Activity Life Cycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You could see this post as well: Android activity life cycle - what are all these methods for?
No, You dont need to write all the lifecycle. But you should have the idea of what lifecycle is going on and what will be the behaviour of Android app. Like why you have to attach activity context to fragment context in onAttach() life cycle method.
What lifecycle will be perform on dialog open or moving from one activity to another??
Read here more.
https://developer.android.com/reference/android/app/Activity.html
Not all, only methods you thing is essential for your task. see docs on Activity's lifecycle: https://developer.android.com/reference/android/app/Activity.html
I found a really strange behavior in the Fragment lifecycle.
After several tests on one Fragment, I realized than I got this execution order every time:
onCreateView()
onStart()
onCreateOptionsMenu()
I am really surprised because I was convinced that onStart would only be called after the execution of onCreateOptionsMenu.
FYI, I was trying to set a MenuItem as a global class variable in onCreateOptionsMenu to be able to use it in onStart. Of course I got a null pointer because of the execution order explained above.
It seems the more I dig into the Fragment's lifecycle, the more I am lost and the documentation only details the main methods, never the ones such as onCreateOptionsMenu, onPrepareOptionsMenu, onCreateContextMenu etc.
Does someone has any additional information about this, the documentation is not really helpful...
Thank you
Check this out https://github.com/xxv/android-lifecycle. There you can see a full lifecycle for both fragment and activity.
I have some problems initializing some values for some views in my Android Activities cause for me to be able to get the views I have to wait for the activity layout aswell as the fragment layout to be inflated until I can initialize the views values. I know i can initialize the views in the fragments OnCreateView but I would like to avoid that and instead have some kind of method that is run right after the onCreates/onCreateView's are done.
Is this possible if so how? And what is the best practice of modifying/initializing views?
Thanks in advance
Unless you have a special case you should not deviate from the lifecycle of a Fragment or Activity.
Anyway if so, Calling a method as you asked for, look at the MainActivity class on this example. It uses a Handler to do work on the UI Thread
You could do this in the onStart() or onResume() methods, which are called after onCreate(), but also after stopping or pausing of the Activity.
For more detailed Information have a look at the Activity Documentation
You should also try to work on the formatting of your question, I'm not sure whether I understand the problem correctly, or not.
You can use the way mentioned over here. It's cleaner.
you put the following method in the fragment.
public void initFragment() { //Do your required things here }
After creating the object for the fragment (for instance example given below)
mFragmentManager = getSupportFragmentManager();
mIssDetailsListFragment = (IssDetailsListFragment) mFragmentManager
.findFragmentById(R.id.fragment_outlet_list);
you call the method initFragment() (Sample code given below)
mIssDetailsListFragment.initFragment()
I use Otto bus to communicate events from activity to the fragments. The problem is, the fragments are managed by the ViewPager and the fragment can be recreated without ever executing its onPause() and/or onResume(). The unpleasant side-effect of that is that when bus.unregister(fragment) is called in onPause() the call periodically fails with IllegalArgumentException. I also suspect that calls to my #Subscribe annotated method can be missed since bus.register() call is in onResume() method which also can be not called. And, unfortunately there's no bus.isRegistered(fragment) method to do a safe check.
Is there a better way to register/unregister ViewPager managed fragment?
I'm aware of this post that discusses a similar setup but it does not include fragments running in ViewPager
If you came here because you have a similar issue - turns out (thanks Nick Campion for pointing it) there's nothing wrong with onPause\onResume in my fragment. I simply had register/untegister called twice: once in the parent abstract class and another one in the child. As in class MyFragment extends AbsFragment. So perhaps look at your code