"java.lang.IllegalStateException: Activity has been destroyed" when press onBackPressed()

14,991

Solution 1

you can just remove this onBackPressed as android implicitly finishes your activity on back key pressed.

And don't call addToBackStack:

ft.addToBackStack(null); 

remove the above line this adds your fragment transaction in backstack and on back key pressed first transaction is reverted and then activity is finished.

If you remove this back press will finish the activity.

change to:

    if(hasFocus){
    frame.postDelayed(new Runnable() {

    @Override
        public void run() {
            // TODO Auto-generated method stub
     fragment = new ViewDetail();
     fragmentManager = getFragmentManager(); 
     ft = fragmentManager.beginTransaction(); 
    ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);
    ft.addToBackStack(null); 
    ft.replace(R.id.container, fragment, "Dettaglio"); 
    ft.commit();//the error is here


        }
    }, 500);
  }

Solution 2

The error is not happening on the line

this.finish();//the error is here

it's most likely happening on

ft.commit();

What happens is that, when you see this error, this code runs after the activity has been destroyed, so there is no FragmentManager or any Activity state to work with, thus the transaction cannot be committed.

This happens because you run this code after a delay, and if you press back the window focus changes and the task gets scheduled, but the activity is destroyed before the task actually starts.

What you could do is add a if (!isFinishing()) check before you run your UI changes inside the task, but I would advise that you rethink your UI flow a little bit. If you want the transaction to run after a delay but before the app finishes, then move the finish() instruction to the end of the task's run() (whether this is UI-wise a good user experience or not).

Note also that you shouldn't have to do both

super.onBackPressed();
this.finish();//the error is here

in onBackPressed(): either you do some custom action (e.g. finishing the app) to replace the default behavior, or you leave it to the original (super) behavior.

Share:
14,991
Adriana C.
Author by

Adriana C.

Updated on June 04, 2022

Comments

  • Adriana C.
    Adriana C. almost 2 years

    I have an activity with an animated fragment. When I press on backpressed to close the activity I get this error. What does it mean?

      01-24 12:41:24.407: E/AndroidRuntime(23621):    FATAL EXCEPTION: main
     01-24 12:41:24.407: E/AndroidRuntime(23621):     java.lang.IllegalStateException: Activity has been destroyed
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1351)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.app.BackStackRecord.commit(BackStackRecord.java:574)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at com.eni.enim4s.TranslucentActivity$1.run(TranslucentActivity.java:70)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.os.Handler.handleCallback(Handler.java:725)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.os.Handler.dispatchMessage(Handler.java:92)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.os.Looper.loop(Looper.java:176)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at android.app.ActivityThread.main(ActivityThread.java:5279)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at java.lang.reflect.Method.invokeNative(Native Method)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at java.lang.reflect.Method.invoke(Method.java:511)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
     01-24 12:41:24.407: E/AndroidRuntime(23621):   at dalvik.system.NativeStart.main(Native Method)
    

    My code:

      public class TranslucentActivity extends Activity {
    
    private Fragment fragment;
    private FrameLayout frame;
    private android.app.FragmentTransaction ft;
    private android.app.FragmentManager fragmentManager;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.translucent);
    
         frame =(FrameLayout)findViewById(R.id.container);
      }
    
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
            frame.postDelayed(new Runnable() {
    
            @Override
                public void run() {
                    // TODO Auto-generated method stub
             fragment = new ViewDetail();
             fragmentManager = getFragmentManager(); 
             ft = fragmentManager.beginTransaction(); 
            ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);
            ft.addToBackStack(null); 
            ft.replace(R.id.container, fragment, "Dettaglio"); 
            ft.commit();//the error is here
    
    
                }
            }, 500);
    }
    
    
    
    
     @Override
         public void onBackPressed() {
        super.onBackPressed();
        this.finish();
       }
    

    }