fragment-lifecycle

IllegalArgumentException with Otto Event bus in Fragment instance

给你一囗甜甜゛ 提交于 2019-12-18 10:55:18
问题 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

Why getContext() in fragment sometimes returns null?

こ雲淡風輕ζ 提交于 2019-12-17 15:54:18
问题 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

Calling notifyDataSetChanged() on my adapter that is within a ListFragment causes a NullPointerException?

我是研究僧i 提交于 2019-12-11 13:18:19
问题 Introduction: I have the following set up: A MainActivity with methods onNewIntent and processNewIntent , the MainActivity also implements FragmentActivity ; and has a tabbed fragment which implements FragmentList A listaddactivity activity that creates a new Parcelable listControlObject The listaddactivity activity sends an intent carrying a parcel which then reconstructs the listControlObject inside of the MainActivity 's onNewIntent and processNewIntent class. However; I can take in the

onDetach not called for fragment?

梦想与她 提交于 2019-12-11 03:21:33
问题 I write an code to launch Activity A to Activity B. Both Activity A and B has fragment implementation. Scenario: If Activity A frequently launch Activity B which contain Fragment, then most of times it missed Fragment.onDetach..I checked with log, normally it give me following override method log: onAttach List item OnCreatView onViewCreate then press device Back Button onPause onStop onDestroyView onDetach now I press device Back button from Activity B which again launch Activity A then it

Pass onActivityResult() data to the same Fragment which is not yet ready

倾然丶 夕夏残阳落幕 提交于 2019-12-11 01:31:57
问题 I am using a Fragment to start a new Activity using startActivityForResult() , I am getting the result (Bundle) in onActivityResult () method.Since onActivityResult () called before onResume ().I want to make sure, I keep/save the Bundle properly so that when Fragment's onResume() gets called, I get the kept/saved result to perform further action. What are the different ways to achieve this. I tried using getArguments()/setArguments(), but that seems to be not the right way to achieve this.

Navigation Component prevent to recreate fragment on back press

陌路散爱 提交于 2019-12-07 03:58:48
问题 I'm using the Jetpack Navigation Component in my project with a signle activity and some fragments. I have a fragment with a list that fills from server side. I call getDataFromServer on onViewCreated method, then when a user clicked on an item new fragment shows. The problem is when I press the back button, onViewCreated calls again in my list fragment. so how can I prevent my first fragment to recreating again? I don't want to onViewCreated calls. 回答1: You can't prevent calling

Navigation Component prevent to recreate fragment on back press

自古美人都是妖i 提交于 2019-12-05 08:11:33
I'm using the Jetpack Navigation Component in my project with a signle activity and some fragments. I have a fragment with a list that fills from server side. I call getDataFromServer on onViewCreated method, then when a user clicked on an item new fragment shows. The problem is when I press the back button, onViewCreated calls again in my list fragment. so how can I prevent my first fragment to recreating again? I don't want to onViewCreated calls. You can't prevent calling onViewCreated method or any method of your fragment when back button pressed so you should better use view model with

Stop handler after the fragment has been destroyed

旧时模样 提交于 2019-12-04 00:09:22
I have a Fragment which sets up a ListView and creates a Handler to update the Listview periodically. However, it looks like the Handler still runs after the Fragment has been destroyed. The following is the code. @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //boilerplate code final Handler handler = new Handler(); handler.post(new Runnable() { @Override public void run() { assignAdapter(); handler.postDelayed(this, 15000); } }); return v; } Updating the ListView after the destruction of the Fragment causes the app to crash. How

Save interface (Listener) in onSaveInstanceState

£可爱£侵袭症+ 提交于 2019-12-03 13:25:30
问题 SaveInstanceState For data like Integer, Long, String and else are fine, I just put it in the bundle and get it back once the onCreateView gets called again. But my fragment also has listener like following, public class SomeFragment extends Fragment { public interface SomeListener { public void onStartDoingSomething(Object whatItIsDoing, Date when); public void onDoneDoingTheThing(Object whatItDid, boolean result); } private SomeFragmentListener listener; private String[] args; public static

Where to restore fragment state that is inside ViewPager

半腔热情 提交于 2019-12-03 13:21:16
问题 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