This is getting really annoying, as I still can't get this working. I have 2 activities, 1st activity opens the camera on clicking an imagebutton, the user takes a photo and the image gets placed in the imagebutton. The 2nd activity changes the background image when a button is clicked. Currently, only the camera application is working as its set as the LAUNCHER in the manifest. I would like to know what INTENT code I add on the MainActivity (1st activity) to make the second activity work. If someone could just possibly post a link or the code of having 1 activity launch another activity, I would be just as happy. Thank you :)
Asked
Active
Viewed 903 times
0
-
check this link, first answer http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android. – Smashing May 13 '15 at 05:21
1 Answers
2
If u don't want any result then:
Intent i = new Intent(Activity.this, SecondActivity.class);
startActivity(i);
and for result,do the following steps:
Intent i = new Intent(Activity.this, SecondActivity.class);
startActivityForResult(i,1);
In your SecondActivity set the data which you want to return back to FirstActivity. If you don't want to return back, don't set any.
For example: In secondActivity if you want to send back data:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
If you don't want to return data:
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
Now in your FirstActivity class write following code for the onActivityResult() method.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
Tamir Abutbul
- 6,886
- 7
- 24
- 48
ammar shahid
- 431
- 4
- 17
-
For the MainActivity, I already have protected void onActivityResult(int requestCode, int resultCode, Intent data) { in my activity which gives me the error that its duplicated, also that the startActivity(i); is giving me the error Return type for the method is missing. Also for the second activity, im getting the error for "result" result cannot be resolved or is not a variable. Thanks for the answer :) – Harrison May 13 '15 at 05:34
-
in the first activity u have to write the function i.e : onActivityResult and have to call the second activity through startActivityForResult and in the second activity u have to pass the result through intents as described. – ammar shahid May 13 '15 at 05:49