Android: Capturing the Return of an Activity

Android: Capturing the return of an activity

I'll focus on answering how to resolve your workround so that it behaves as you want.

To capture actions performed on one Activity within another requires three steps.

Launch the secondary Activity (your 'camera Activity') as a subactivity by using startActivityForResult instead of startActivity.

Intent i = new Intent(this,CameraActivity.class);    
startActivityForResult(i, STATIC_INTEGER_VALUE);

Within the subactivity (camera Activity), rather than just closing the Activity when a user clicks the different tab image, you need to create a new Intent and include the index of the tab to display when you return to the parent app using the extras bundle. To pass it back to the parent call setResult before calling finish to close the camera Activity.

resultIntent = new Intent(null);
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, tabIndexValue);
setResult(Activity.RESULT_OK, resultIntent);
finish();

The final step is in the calling Activity, override onActivityResult to listen for callbacks from the camera Activity. Get the extra from the returned Intent to determine the index of the tab you should be displaying.

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (STATIC_INTEGER_VALUE) : {
if (resultCode == Activity.RESULT_OK) {
int tabIndex = data.getIntExtra(PUBLIC_STATIC_STRING_IDENTIFIER);
// TODO Switch tabs using the index.
}
break;
}
}
}

return text from other activity

You can use startActivityForResult() method if you need to pass data back from an activity.
E.x: return data from Activity B

Activity A:

Step1:Call StartActivityForResult() method

Intent i = new Intent(A.this,B.class);
// use startActivityForResult(Intent,request_code) method()
//with request_code is used to identify.
starActivityForResult(i,1)

Step2:
You must implements onActivityResult(int requestCode,int resultCode,Intent data) method

//check requestCode and resultCode    
if(requestCode==1)
{

if(resultCode==RESULT_OK)

{
//get Data
String temp = data.getData().toString();
}

}

Activity B:

 //set Data return Activity B at anywhere you want
Intent data = new Intent()
data.setData("String_Test");

setResult(RESULT_OK,data)
//close the activity
finish();

Note: instead of using setData method(), you also able to use putExtras(Bunble object) to send data.

How to return results from one activity to another using an Intent in Android?

You should put anything you need in the result intent and then get them from extras.

just do it like this :

in Splash:

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent data = new Intent();
data.putExtra("n1", display.getText().toString());
dat.putExtra("n2", disp.getText().toString());
setResult(RESULT_OK, dat);

finish();

}
});

and in MainActivity:

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == code) {
if (resultCode == RESULT_OK) {

// Toast.makeText(this, data.getData().toString(),
//Toast.LENGTH_SHORT).show();
a = data.getStringExtra("n1");

num = Integer.parseInt(a);
t.setText("" + num);
c = data.StringExtra("n2");
num1 = Integer.parseInt(c);
t.setText("" + num1);
int total = num1 + num;

}
}
}

How to catch event of coming back to an activity after hitting back

Use onResume() method in your main activity or Use startActivityForResult method in your activity by overriding the keyDown method in sub activities,it may help you

startActivityForResult for capturing image via the camera returns diffrent requestcode?

I've had the same problem. Create a public static final int in each Activity and use the correct one as requestCode. Also, if you are calling startActivityForResult(...) from a Fragment, try changing it with 'getActivity().startActivityForResult(...)'

how to move back to a particular activity after Image capturing in android

Use this for Taking Photo and store it in Sdcard :

public void takePhoto() {

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File folder = new File(Environment.getExternalStorageDirectory() + "/Photo");
boolean success = false;
if(!folder.exists()){
success = folder.mkdir();
}
final Calendar c = Calendar.getInstance();
String path=String.format("/sdcard/Photo/%s.png","Photos");
photo = new File(path);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
}

Use onActivityResult Method :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
// Use your server Post Coding :

// Get the Rsponse as String and compare here

if(response.equalsignoreCase("POST SUCCESS")){
startActivity(new Activity (CurrentActivity.this,Activity2.class))
}
super.onActivityResult(requestCode, resultCode, data);
}
}

After the Image captured from Camera its will come to onActivityResult() method Check the Response and Redirect the Page Accordingly.



Related Topics



Leave a reply



Submit