Search code examples
androidmethodsexecutionsequential

Method call terminates in between and jumps to the next method call


I'm trying to build a notepad application in android. I'm in this situation, when the user chooses to create a new file while still working on a file, the application should first ask for a save and then restart the MainActivity to create the new file.

So, basically, the sequence is as follows:

->Working on current file
->Clicks on New File
->Save dialog
->Restart MainActivity.

For this, I've been using a method called new file as below:

public void new_file()
    {
        save_file();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

The problem here is that, everytime I run the application and text this part of the code, the dialog pops up and immediately a new file.

I would like to know how I can first let the Activity finish the save_file() method call and then the Activity Restarts.

Thanks in Advance


Solution

  • It looks like it works fine, but not seeing safe_file() code (BTW: consider switching to more standard function naming convention) it's hard to be sure , but note dialogs are asynchronous, so you cannot start dialog and expect all further code to wait for dialog dismiss. I assume you popup dialog in safe_file() method, so you should either call back to start new activity from there when done saving or you should do that in your dialog click listener.