Created
May 12, 2020 22:40
-
-
Save magdamiu/0fe027462ad5fcb8fbbbbd692e16b8d4 to your computer and use it in GitHub Desktop.
startActivityForResult() sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Activity1 extends AppCompatActivity { | |
public static final int REQUEST_CODE = 255; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_1); | |
} | |
public void sendMessageToActivityOnClick(View view) { | |
Intent intent = new Intent(Activity1.this, Activity2.class); | |
intent.putExtra(MESSAGE, "Are you there?"); | |
startActivityForResult(intent, REQUEST_CODE); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { | |
String resultFromActivity2 = data.getStringExtra(RESULT); | |
// use the result | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment