fragment-lifecycle

Where to restore fragment state that is inside ViewPager

懵懂的女人 提交于 2019-12-03 03:27:51
Short Version: I have an Activity that has a ViewPager. The ViewPager has three fragments inside it. I am storing the data inside the fragments by implementing Parcelable and storing it inside the bundle. Now the question is where do I restore the data. I am confused because (from what I know) the ViewPager is creating a new instance of the fragment each time I rotate the screen. (A new activity is created -> new ViewPager -> new Fragment). Please do correct me if I am wrong. Long version: My ViewPager inside MainActivity ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

IllegalArgumentException with Otto Event bus in Fragment instance

佐手、 提交于 2019-11-30 00:36:14
I am using Otto Event bus to subscribe to certain events in a ListFragment. The bus instance is stored and created in an subclass of Application, in other words, it bus should work as a singleton. It seems like this is not a case... The fragment is registering to the bus in onActivityCreated(Bundle) and unregistering in onDestroy() . This does not work as it should. I have gotten several crash reports from devices where the app crashes when calling unregister() (java.lang.IllegalArgumentException: Missing event handler for an annotated method...). This exception is only thrown if unregister()

Why getContext() in fragment sometimes returns null?

天涯浪子 提交于 2019-11-27 20:33:01
Why getContext() sometimes returns null ? I pass context to LastNewsRVAdapter.java as an argument. But LayoutInflater.from(context) sometimes crashes. I'm getting a few crash reports on play console. Below is crash report. java.lang.NullPointerException com.example.adapters.LastNewsRVAdapter.<init> java.lang.NullPointerException: at android.view.LayoutInflater.from (LayoutInflater.java:211) at com.example.adapters.LastNewsRVAdapter.<init> (LastNewsRVAdapter.java) at com.example.fragments.MainFragment$2.onFailure (MainFragment.java) or .onResponse (MainFragment.java) at retrofit2

Difference between add() & replace() with Fragment's lifecycle

一曲冷凌霜 提交于 2019-11-27 03:09:14
问题 My program has 6 fragments: Fragment1, Fragment2,....->Fragment6. I use the add() and replace() statement to switch between the fragment and track their lifecycle. Fragment1 add Fragment2 add Fragment3 add Fragment4 add Fragment5 replace Fragment6 The log-cat to shown their lifecycle (i have some printf-points in onCreate, onCreateView, onDestroyView, onDestroy for tracking) Tag __ _ __ _ __ _ __ _ __ Text Fragment1_ _ __ _ ___ _onCreate Fragment1_ _ __ _ ___ _onCreateView Fragment1_ _ __ _ _

Restoring MapView's state on rotate and on back

痴心易碎 提交于 2019-11-27 00:07:16
问题 Background I have a larger application in which I had/have several problems with new Google Maps API. I tried to describe it in a different question but since it seems too complex I decided to start a new project, as simple as possible and try to reproduce problems. So here it is. The situation I'm using Fragments and want to put MapView inside. I don't want to use MapFragment . The sample project I prepared may be not very beautiful but I tried to make it as simple as possible and it had to

Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

此生再无相见时 提交于 2019-11-26 00:39:07
问题 What are the differences between onCreate() , onCreateView() , and onActivityCreated() in fragments and what would they each be used for? 回答1: onCreate(): The onCreate() method in a Fragment is called after the Activity 's onAttachFragment() but before that Fragment 's onCreateView() . In this method, you can assign variables, get Intent extras, and anything else that doesn't involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be called when the