Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

android - OnActivityResult is not working in TabActivityGroup?

I am using Tab Activity as a main Activity in which it has 4 tabs. One tab is Activity Group which has three buttons. Each button is one activity. I am using camera in one of those activity. I know its working because i have checked in the memory card the image get saved, but it doesn't calling the Onactivityresult() method.

This is to display the ContentView

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.enter_expenses, null);
    this.setContentView(viewToLoad);            

This code is used to take the image

             dbimgguid = UUID.randomUUID();
             imagename =dbimgguid.toString();
             Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
             photo = new File(Environment.getExternalStorageDirectory(), "Expenses"+imagename+".jpg");
             intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
             imageurl = Uri.fromFile(photo); 
             startActivityForResult(intent, CAMERA_RECEIPTREQUEST); 

This is onactivity result

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
     super.onActivityResult(requestCode, resultCode, data);  
     System.out.println(requestCode);
     System.out.println(resultCode);
     switch(requestCode)
     {
     case CAMERA_RECEIPTREQUEST:  
         if(resultCode== Activity.RESULT_OK)
         {
         //Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inSampleSize = 8;
         //ImageView jpgView = (ImageView)findViewById(R.id.imageView1);
         Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options);  
         String image = "Expenses"+imagename+".jpg"; 
         Intent imagepass = new Intent(ExpensesActivity.this,ReviewReceiptImage.class);
         imagepass.putExtra("receipt", receipt);
         imagepass.putExtra("imagename", image);
         startActivityForResult(imagepass, CAMERA_CONFIRMRECEIPT); 
         Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         }

     case CAMERA_CONFIRMRECEIPT:

         Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         if(resultCode == CAMERA_CONFIRMRECEIPT)
         {
             take_receipt.setEnabled(false);
             take_receipt.setVisibility(View.GONE);
             show_receipt.setEnabled(true);
             show_receipt.setVisibility(View.VISIBLE);
         } 
         break;
     }
 }

Thanks for your help guys

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have solved my issue.Please see my answer below,

I finally figured out/remembered that the issue is because Android will only allow a nested layer of sub-activities...ie sub-activities can't nest sub-activitites. To solve this:

call getParent().startActivityForResult() from your sub-activity your parent (the activitygroup) will be able to handle the onActivityResult. So I created a subclass of ActivityGroup and handled this onActivityResult. You can re-route that result back to the sub-activity if you need to. Just get the current activity by getLocalActivityManager().getCurrentActivity() . My sub-activities inherit from a custom activity so I added a handleActivityResult(requestCode, resultCode, data) in that subclass for the ActivityGroup to call.

Answer for the above question with exact code

ActivityGroup->SubActivities->onActivityResult->ActivityGroup(onActivityResult)->subActivity(onActivityResult)

This is the ActivityGroup Class in which you have to define onActivityResult for all sub-activities,

@Override
public void onActivityResult(int requestCode,int resultCode, Intent data)
{

         switch(requestCode)
            {

            case CAMERA_RECEIPTREQUEST:   
            if(resultCode == Activity.RESULT_OK)
            {
            ExpensesActivity activity = (ExpensesActivity) getLocalActivityManager().getCurrentActivity();
            activity.onActivityResult(requestCode, resultCode, data);
            }
            break;

            case CAMERA_CONFIRMRECEIPT:
            ExpensesActivity activity1 = (ExpensesActivity) getLocalActivityManager().getCurrentActivity();
            activity1.onActivityResult(requestCode, resultCode, data);   
            }

        }

In Sub Activity, you have to set the view as below

    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.enter_time, null);
    this.setContentView(viewToLoad);

Then in Sub- Activity onActivityResulty()

    @Override
    public void onActivityResult(int requestCode,int resultCode, Intent data)
    {
     //Some Code
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...