Android: Capturing the return of an activity

前端 未结 1 1326
长情又很酷
长情又很酷 2020-11-28 02:38

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 r         


        
相关标签:
1条回答
  • 2020-11-28 03:05

    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; 
        } 
      } 
    } 
    
    0 讨论(0)
提交回复
热议问题