KarenElissa
KarenElissa

Reputation: 101

What is the difference between these two ways to start an activity?

First off, I'm totally new at all of this and am mostly learning be searching the internet for directions on how to do what I want and then figuring out how to use it.

So I've found these two versions of how to start an activity, but I don't really understand the difference. Is one better than the other? Or should they be used in different circumstances? Or are they just two different ways of doing the same thing?

Button home = (Button) findViewById(R.id.to_home);
    home.setOnClickListener (new View.OnClickListener() {
        public void onClick(View view) {
            Intent i = new Intent(view.getContext(), Home.class);
                startActivityForResult (i, 0);

        }
    });

or this one

Button button = (Button)findViewById(R.id.b_cup);
    button.setOnClickListener (new View.OnClickListener() {
        public void onClick (View view) {
            Intent i = new Intent (Home.this, Cup.class);
            startActivity (i);

        }
    });

Upvotes: 1

Views: 893

Answers (2)

Jay Dee
Jay Dee

Reputation: 364

Using the startIntentForResult(Intent,RequestCode) is extremely useful though. when the Intent that has been called closes then this is called

@Override protected void onActivityResult ( int request_code, int result_code, Intent i ) {

super.onActivityResult( request_code, result_code, i );

}

The Result code can then be used to see if the intent was cancelled or completed successfully. Also if you are calling several Intents from the main activity specifying a unique request code will let you handle intent results unique to the intents purpose eg

startIntentForResult(Intent1,1)

...

startIntentForResult(Intent2,2)

...

startIntentForResult(Intent3,3)

...

Then in the onActivityReslut:

@Override protected void onActivityResult ( int request_code, int result_code, Intent i ) {

if (result_code == RESULT_OK){

    switch (request_code)
    {
    case 1:
        //Do Something
        break;
    case 2:
        //Do Something
        break;
    case 3:
        //Do Something
        break;
    default:
        //Unrecognised request_code
    }
}

super.onActivityResult( request_code, result_code, i );

}

startIntenetForResult is very very useful.

Upvotes: 0

Matt
Matt

Reputation: 3847

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

Unless you want the new activity to return something, or you need to know when it finishes, startActivity() will be just fine.

See: http://developer.android.com/reference/android/app/Activity.html

Upvotes: 7

Related Questions