UPD

Activity

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_screen_activity); mainFragment = new MainScreenFragment(); getSupportFragmentManager() .beginTransaction() .replace(R.id.main_frame, mainFragment) .commit(); } 

Fragment

  @Override public void onResume() { super.onResume(); 1: redraw(); 2: getAccidents(); } private void redraw(){ Accidents.getVisible() .subscribe(a -> listContent.addView( 3: Rows.getAccidentRow(getActivity(), listContent, a) )); } private void getAccidents() { startRefreshAnimation(); //ProgressBar on Accidents.updated //подписка на Subject<Boolean> .observeOn(AndroidSchedulers.mainThread()) //Для ProgressBar .subscribe(b -> { stopRefreshAnimation(); //ProgressBar off redraw(); }); Accidents.update(); //запуск сетевого запроса, который по окончанию зашлет true в подписку строкой выше } 

In steps:

  1. Running the application. Everything works out without problems.
  2. I close the application with the back button.
  3. I restart.
  4. Line 1: executes.
  5. Line 2: fulfills, but in line 3: this time getActivity() returns null

Actually the question is why with the same code the jamb occurs exactly when the application is restarted?

  • Perhaps you still have the first fragment in memory and with the next step you change it to the second one. Try activating onCreate when adding a fragment to check if it has already been added. For example: getSupportFragmentManager().findFragmentById(R.id.main_frame) != null - Juriy Spb
  • On the schematic there is no need to spit saliva, no matter what is behind this action, the call of the life-cycle callbacks occurs strictly sequentially, according to the schematic. Assuming that they are called "as horrible" (asynchronously) speaks only of a lack of understanding of what is happening. - pavlofff
  • @pavlofff so explain why after onCreateActivity and onAttach getActivity() returns null - rjhdby
  • 2
    There is no reason for the behavior described in the code in your question. The problem for this code is not reproduced. Obviously the problem is elsewhere. - pavlofff
  • I propose first to transfer the getActivity() receipt to the getActivity() method onResume() and assign the class field there. In the future, refer to this field, and not through the method. If it does not help, I propose to exclude calls to RxJava constants and check the status of the getActivity() method, I am pretty sure that there will be no problem. If so, then the problem must be sought in the Rx itself - pavlofff

2 answers 2

You need to add Fragment to the Activity only when you first start the Activity. You can check for the first time whether the Activity is launched or not through the onCreate method's savedInstanceState parameter, if it is null, then you can add a snippet.

Change the onCreate method of your Activity:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_screen_activity); if (savedInstanceState == null) { mainFragment = new MainScreenFragment(); getSupportFragmentManager() .beginTransaction() .replace(R.id.main_frame, mainFragment) .commit(); } } 
  • @rjhdby No, a fragment was created earlier or not is not a problem in the question. Especially because when you close the button "back", the activation (and the classes created by it, including the fragment) is destroyed. Obviously, the problem with RxJava, but I do not work with it and understand why this is not going to help. - pavlofff
  • one
    The getActivity method can return null only if the Fragment is not attached to any Activity. In which case this can happen, I wrote in the answer above. Check or Fragment attached to the Activity via the Fragment.isAdded () method. - couldDog
  • It did not help, the same error - rjhdby

Workaround

It made me work, but the solution raises more questions than it gives answers.

In short, I organized a subscription to the context from the onAttach() method

 private FragmentActivity mActivity; public static Subject<Context> isAttached = PublishSubject.create(); @Override public void onAttach(Context context) { super.onAttach(context); mActivity = (FragmentActivity) context; isAttached.onNext(mActivity); } public void redraw() { isAttached.subscribe(context -> { listContent.removeAllViews(); Log.e("FRAGMENT", String.valueOf(this.isAdded())); Accidents.getVisible() .sorted((a, b) -> MyUtils.reverseComparator(a.id, b.id)) .subscribe(a -> listContent.addView(Rows.getAccidentRow(context, listContent, a))); map.placeAccidents(context); } ); if (mActivity != null) isAttached.onNext(mActivity); } 

And Log.e("FRAGMENT", String.valueOf(this.isAdded())); returns false at the time the context already exists. I suspect that somehow all this mysticism is connected with the restoration from the savedInstanceState but I can not prove