Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Capturing the return of an activity

I have a question regarding launching new activities. It boils down to this. I have 3 tabs on a view

A) contains gMap activity  B) camera activity  C) some random text fields. 

Requirement is that the application runs in Portrait mode.

All 3 tabs work as expected w/ the exception of the Camera Preview Surface (B). It is rotated 90 degrees. They only way to make it correct is to set the app to landscape which throws all my tabs around, and is pretty much unworkable.

My solution is this : replace

my camera activity with a regular activity that is empty w/ the exception of

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

This launches my CameraActivity. And that works fine. I had to do a linear layout and include 3 images that look like real tabs, so I can try and mimic the operation of the tabs while rotating the screen to landscape and keep the visuals as portrait. The user can click one of the images(buttons) to display the next tab. This is my issue. It should exit my 'camera activity' returning to the 'blank activity' in a tab, where it should be interpreted to click the desiered tab from my image.

The main thing is, when it returns, it returns to a blank (black) page under a tab (because it is 'empty'). How can I capture the return event back to the page that called the activity, and then see what action they performed?

I can set an onclicklistener where I can respond to the fake tabs (images) being clicked to exit out of the camera activity. On exit, the tab should update so that is where you return. any Suggestions?

Thanks,

like image 661
Chrispix Avatar asked Jan 16 '09 04:01

Chrispix


People also ask

How do you get a response from an activity in Android?

You must call the second activity using the startActivityForResult method. In your second activity, when it is finished, you can execute the setResult method where basically you put the result information. Then, on your first activity, you override the onActivityResult method.

How can I get back the result from one activity to main activity or other activity?

Put the data that you want to send back to the previous activity into an Intent . The data is stored in the Intent using a key-value pair. Set the result to RESULT_OK and add the intent holding your data. Call finish() to close the Second Activity.

What is the use of onActivityResult in Android?

When you done with the subsequent activity and returns, the system calls your activity's onActivityResult() method. This method includes three arguments: @The request code you passed to startActivityForResult() . @A result code specified by the second activity.

How can I get result from startActivityForResult?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.


1 Answers

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;      }    }  }  
like image 82
Reto Meier Avatar answered Oct 11 '22 12:10

Reto Meier