stuart-d2
3/5/2014 - 8:08 PM

Request Code / Request Status See also "Getting a Result From an activity" developer.android.com/training/basics/intents/result.html A reque

Request Code / Request Status See also "Getting a Result From an activity" developer.android.com/training/basics/intents/result.html A request code's purpose is to match the result of a "startActivityForResult" with the type of the original request. Request Code is an integer that identifies your request. On receiving the resultant Intent, request code is provide so the app can properly identify the result and see how to handle it.
RESULT_OK and RESULT_CANCELED do not have to be declared as field variables. Inherited.


// 1. Declare Request Code field in main activity

private static final int <<requestCodeName>> = 0;

//2. Bind request code to the startActivityForResult method. 
<<inflatedView>>.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                log("Entered footerView.OnClickListener.onClick()");

                //TODO - Attach Listener to inflatedView . Implement onClick().
                Intent startNewActivity = new Intent(<<mainActivityCLass>>.this, <<secondaryActivityClass>>.class);
                startActivityForResult(startNewActivity,<<requestCodeName>>);

            }
        });

//Secondary Activity 
//3.  Bind Result Codes to onCLickListeners using setResult() and finally return the intent. 

final Button cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				log("Entered cancelButton.OnClickListener.onClick()");

				  
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	

	final Button submitButton = (Button) findViewById(R.id.submitButton);
		submitButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
      	//TODO - return data Intent and finish
				setResult(Activity.RESULT_OK, data);
				finish();
			}
		});
	}
      
      		

//MainActivity  
//Todo Check result code and request code. Encapsulate within onActivityRestult method.  
@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	
	
		 if(resultCode == RESULT_OK) {
             if(requestCode == <<requestCodeName>>) {
                //ToDo if Result Code OK  
                     mAdapter.add(new ToDoItem(data));
             } }
  }