Question: I am currently modifying this tutorial from <https://www.simplifiedios.net/swift-php-mysql-tutorial/> . The purpose of the program is to close a ViewController if the correct message is recieved and to display a UIAltertAction if an invalid message is recieved. Could there be something that i am missing that "notChangingVariable" is never changing?

```
    ...IBACTION...
    var notChangingVariable: Int

    notChangingVariable = 0
    ...othervariablesdeclared...
    let task = URLSession.shared.dataTask(with: request as URLRequest){
        data, response, error in
        if(error != nil){
            return;
        }

        //parsing the response
        do{
            //converting resonse to NSDictionary
            let myJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            //parsing the json
            if let parseJSON = myJSON{
                var msg : String!
                //getting the json response
                msg = parseJSON["message"] as! String?
                if(msg == "SameTeam"){
                    print(msg)
                    notChangingVariable  = 1
                    print(notChangingVariable)
                }
            }
        }catch{
            print("error32 is \(error)")
        }
    }
    task.resume()
    print(notChangingVariable) //IS 0

    ...FURTHER USE notChangingVariable...

```

Answer: 
Just use `addToBackStack(null)` before commit when you make fragment transactions and then when you press the back button you will get back to your fragment.
Old question, but anyone looking for an answer to this can just do this:

```
@Override
public void onBackPressed() {
  finish();
}

```

`finish()` "closes" the current Activity and takes you back to the Fragment (Actually, it takes you back to the Activity hosting the Fragment)