This question already has answers here:
Finishing current activity from a fragment
(13 answers)
Closed 9 years ago.
What is the equivalent of
finish()
for a fragment activity in
OnPause();
I cant find it anywhere, I just want the fragment to end when it is paused
As others have already stated, call finish() as you would in a regular Activity. In regards to the onPause() part of your question, I'm not sure what you are looking for, but it might be a good idea to review the lifecycles of Activities and Fragments...
Activity Lifecycle
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Fragment Lifecycle
http://developer.android.com/reference/android/app/Fragment.html#Lifecycle
call this line and it will kill the current fragment
getActivity().finish();
You can still use finish(). FragmentActivity extends Activity
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
A FragmentActivity is extended from an Activity, so you can still use the finish() call to end your class.
Related
This question already has answers here:
Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments
(3 answers)
Closed 6 years ago.
I am confusing onCreate() method in both Activity and Fragment. What onCreate() will do in Fragment and What are differences onCreate() method between Activity and Fragment.Please help me i am learning by myself.
Thanks......
onCreate()
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
I have read both solutions here:
Prevent Activity Stack from being Restored?
But I hope there is a more elegant way to solve this.
Here is where my problem originates.
I have an Activity that has a View Pager with Fragments
Those fragments are inner classes and for some reason inner Fragments should be static.
The fragments however, use a lot of stuff from the parent Activity, that is not static, and cannot be accessed from a static context. I tried passing them as arguments, but one of the arguments is the ViewPager itself and it cannot be serialized or accessed from a static context.
If the app has been idle for hours, the Activity is recreated instead of started fresh, and the non-static inner class Fragment crashes.
My activity does not need to remember its state from 5 hours ago. I want it to not be restored whenever the app is brought back to foreground again.
I think I found the solution, it is so simple! (a bit hacky though)
#Override
protected void onSaveInstanceState(Bundle outState) {
}
No call to super, so the OS has no information to use when attempting to restore the activity state, so it builds it anew altoghether.
This question already has answers here:
What is the difference between Fragment and FragmentActivity?
(4 answers)
Closed 9 years ago.
I am new in Android programming. I want to do a Fragment, I have seen examples that uses Fragment and FragmentActivity, What's the difference between them and which cases should be used each one?
Thanks
FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.
Well Fragment is good component that copy the basic behaviors of Activity, still not a stand-alone application component like Activity and needs to be attached to Activity in order to work.
Read here Difference between Fragment And FragmentActivity
FragmentActivity is part of the google-support-v4 lib which is basically adds a Fragment support to system with OS under 2.3. So FragmentActivity is exactly as a simple Activity only it gives you the ability to add Fragment to it.
Fragment is an object that shares parts of the Activity life cycle and can be added as part of you UI to an Activity or FragmentActivity with it's logic. the beauty of Fragments is that can be reused across different Activities in your application.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ViewPager Activity to notify a Fragment of a specific event
I am experimenting with using fragments and I ran into this problem.
I have a service running in the background which I want to update a listview in a fragment.
First I just wanted to have the fragment listen for a broadcast, but it seems like registerReceiver is not allowed in a fragment?
How do I do this. Do I need to listen for a broadcast in the main activity and then somhow send it to the fragment?
I'd use the activity for this. Google has some good examples and description of the topic fragment and activity communication. You can read about it here: http://developer.android.com/training/basics/fragments/communicating.html
In short, fragments use listeners to communicate with activities and activities just use public methodes in fragments.
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.