Search code examples
androidandroid-fragmentsandroid-lifecycle

How can I skip onResume while starting of an Fragment?


Basically I am looking to place some code in OnResume() which gets triggered only when a user pressses back from an activity that was called from this fragment but that code inside onResume() should not get executed on Start


Solution

  • You can use startActivityForResult to start activity and onActivityResult will be called when you come back.

    If you want the behavior you mentioned in onResume anyway, you may use a boolean flag:

    boolean started = false;
    
    @Override
    public void onResume() {
        super.onResume();
        if(started) {
            //do your task
        } else {
            started = true;
        }
    }
    

    But onActivityResult is the good way to do this.