Getting the error “Java.lang.IllegalStateException Activity has been destroyed” when using tabs with ViewPager

前端 未结 13 1892
再見小時候
再見小時候 2020-11-22 11:53

I have an application that consists of using ActionBarSherlock in tab mode.I have 5 tabs and the content of each tab is handled using fragments. For tab2 though, I have a fr

相关标签:
13条回答
  • 2020-11-22 12:20

    I had this problem and I couldn't find the solution here, so I want to share my solution in case someone else has this problem again.

    I had this code:

    public void finishAction() {
      onDestroy();
      finish();
    }
    

    and solved the problem by deleting the line "onDestroy();"

    public void finishAction() {
      finish();
    }
    

    The reason I wrote the initial code: I know that when you execute "finish()" the activity calls "onDestroy()", but I'm using threads and I wanted to ensure that all the threads are destroyed before starting the next activity, and it looks like "finish()" is not always immediate. I need to process/reduce a lot of “Bitmap” and display big “bitmaps” and I’m working on improving the use of the memory in my app

    Now I will kill the threads using a different method and I’ll execute this method from "onDestroy();" and when I think I need to kill all the threads.

    public void finishAction() {
      onDestroyThreads();
      finish();
    }
    
    0 讨论(0)
  • 2020-11-22 12:21

    I force the fragment containing the child fragment to NULL in onPause and it fixes my problem

    fragment = null;
    
    0 讨论(0)
  • 2020-11-22 12:22

    I encountered the same issue when calling super.onCreate() at the end of my method. The reason: attachActivity() is called in onCreate() of FragmentActivity. When overriding onCreate() and, for example, creating tabs, the Tab manager will try to switch to a fragment while not having the activity attached to the FragmentManager.

    Simple solution: Move the call to super.onCreate() to the head of the function body.

    In general, it seems there are loads of reasons this issue may occur. This is just another one ...

    Matthias

    0 讨论(0)
  • 2020-11-22 12:23

    I got the very same error when trying to access the child FragmentManager before the fragment was fully initialized (i.e. attached to the Activity or at least onCreateView() called). Else the FragmentManager gets initialized with a null Activity causing the aforementioned exception.

    0 讨论(0)
  • 2020-11-22 12:24

    I encountered the same issue and lateron found out that, I have missed call to super.onCreate( savedInstanceState ); in onCreate() of FragmentActivity.

    0 讨论(0)
  • 2020-11-22 12:29

    I had this error because I was using LocalBroadcastManager and I did:

    unregisterReceiver(intentReloadFragmentReceiver);
    

    instead of:

    LocalBroadcastManager.getInstance(this).unregisterReceiver(intentReloadFragmentReceiver);
    
    0 讨论(0)
提交回复
热议问题