0

I am unable to understand when to use these methods

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

            }
        });

and

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

        }
    });

and

my other doubt is when i start an new Activity using intent onCreate(Bundle b) method is called and i want to know whether the b(Bundle) refers to current Activity or the the previous Activity the one which is called.

Mudassir
  • 13,031
  • 8
  • 59
  • 87
user493244
  • 909
  • 7
  • 19

3 Answers3

3

OnstartActivityforResult is used when we need some data from secondActivity Like as camera ,when we click and it come back on first Activity onActivityForResult method . And startActivity is just for start new Activity in Android...

http://rahulonblog.blogspot.in/2010/05/android-startactivityforresult-example.html

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
1

If you call startActivityForResult in any activity, the current activity which is calling will be notified when the called activity is finished or back button pressed, and some information will be returned to calling activity.

The calling activity's onActivityResult method is responsible to receive the information sent by finished activity.

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
0

The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.

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.

When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), along with the integer identifier it originally supplied.

If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.

  • So, you need to use startActivity(), if you only want to start a new activity,but if you want to get some result (may be some value, or some String), you need to use startActivityForResult(). –  Feb 01 '12 at 13:06