3

I'm working on an Android app. I have an Activity that checks to see if an XML file exists on my device. If it doesn't exist, I call a routine that does a bunch of stuff, including downloading that file from a URL.

If it does exist, I want to prompt (Yes/No) the user to see if they want to re-download the file, or just skip it.

I started by using a Dialog. The discussions I saw said that Dialogs are only asynchronous, so I switched to using an Activity to prompt for Yes/No.

After further reading, I believe the real answer is that starting an Activity using startActivity() sets off the Activity asynchronously, but using startActivityForResult() sets it off synchronously (blocking). Is this statement correct?

OK, assuming my statement above is correct, I have been looking into how to get back to my original starting point in the code flow, knowing which button (Yes/No) was selected by the user.

All of the examples show me that I need to get my result using this.

protected void onActivityResult(int requestCode, int resultCode,Intent data) {

}

I am struggling with how to put this into my code so that it flows easier. I see plenty of examples that show the pieces of code needed, but I'm not clear on how it all fits together. Does my code structure end up like this:

public class MainScreen extends Activity    {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected void onButtonClick(parameters)    {
        /* Do some work to see if the file exists */
        if (!file.exists()) {
            runComplexRoutine(various_parameters);

        } else {
            Intent myIntent = new Intent(getBaseContext(), PromptingActivity.class);
            myIntent.putExtra("filename", variable_holding_filename);   // Just passing a parameter to use in the title of the caled Activity
            startActivityForResult(myIntent, 1);
                        // Point A
        }
    }


    protected void onActivityResult(int requestCode, int resultCode,Intent data) {
        if (resultsCode == Activity.RESULT_OK)  {
            runComplexRoutine(various_parameters);
        }
        // else, do nothing...
    }

}

My problem with this is that I have to pass lots of various_parameters around. It would be great if I could resume flow at Point A, but that doesn't seem possible. Am I correct here?

Chuck
  • 203
  • 6
  • 16

2 Answers2

1

startActivityForResult() is asynchronous. It can feel synchronous to the user since the UI will change and your calling activity will be paused (your onPause() method will be called).

Your calling activity however is still able to run code; you'll implement a callback to be called when the activity you started for result is completed (which makes it quite like a dialog).

mah
  • 39,056
  • 9
  • 76
  • 93
  • I see what you are saying in the first statement. In the second statement, am I limited to returning to the `onActivityResult()` routine, or can I return to the point where I left (right after `startActivityForResult()`)? My concern is that I have quite a few parameters that would need to be passed to the called Activity, then back to the calling routine. – Chuck Aug 28 '12 at 20:41
  • And, can you point me to a "complete" example, rather than ones that jsut show the pieces? Thanks!!! – Chuck Aug 28 '12 at 20:46
  • When your activity calls `startActivityForResult()` your code will not block, so you have no way to return to the point where you left off. I think you're thinking of linear programming though, which really is not the way UI development works... you need to think more event-driven. Some event (`onCreate()`, perhaps) caused your code to run. Your event processing function will run to completion before you can receive another event (such as the result of your child activity). – mah Aug 28 '12 at 20:48
  • http://joshhendo.blogspot.com/2011/04/android-using-startactivityforresult.html has an example. Take notice of the call to `setResult()` in the child activity, and `onActivityResult()` in the parent to receive results. Note also that you can pass a Bundle in the intent as an extra to give the child its parameters, and the child can set a Bundle as its result to pass back data. – mah Aug 28 '12 at 20:50
  • I agree about the "linear thinking" comment. Most of my exerience is with .NET and PHP. :-) I'll look at that example. Thanks very much!!! – Chuck Aug 28 '12 at 20:55
0

I found that using startActivityForResult and onActivityResult will move flow after startActivityForResult to onActivityResult and break flow of startActivityForResult. I thought about how to continue flow after startActivityForResult without breaking it. While thinking about appropriate method, I finally found the content of "Getting a result from an activity". Hope this helps.

Here's a link!

siprikorea
  • 63
  • 1
  • 9