Skip to content

Instantly share code, notes, and snippets.

@csdear
Created March 5, 2014 20:08
Show Gist options
  • Select an option

  • Save csdear/5e31bd05de38844a7291 to your computer and use it in GitHub Desktop.

Select an option

Save csdear/5e31bd05de38844a7291 to your computer and use it in GitHub Desktop.
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, reque…
// 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));
} }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment