使用带Tab的Activitygroup不显示第二个活动

时间:2013-09-17 10:48:36

标签: android tabs activitygroup

在我的应用中,我'使用Tab。有三个选项卡。其中第三个选项卡是具有两个活动的活动组。在第一个活动中,用户有两种选择。用户可以从相机或图库中选择图像。选择图像后,用户应移至子活动,这将显示该活动中的所选图像。以下是我的代码......

public class GalleryPic extends Activity {

    private static final int TAKE_PICTURE = 0;
    private static final int REQUEST_ID = 1;
    private static final int HALF = 2;
    private Uri mUri;
    private Bitmap mPhoto;
    ImageView ivGallery, ivCamera;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery_pic);
        ivGallery = (ImageView)findViewById(R.id.gallery);
        ivCamera = (ImageView)findViewById(R.id.snap);

        ivGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                Intent in = new Intent(getParent(), GalleryPic_Second.class);
                TabGroupActivity parent = (TabGroupActivity) getParent();
                parent.startChildActivity("ArrowsActivity", in);
                startActivityForResult(intent, REQUEST_ID);

            }
        });

        ivCamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
                File f = new File(Environment.getExternalStorageDirectory(),  "photo.jpg");
                i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                mUri = Uri.fromFile(f);
                Intent in = new Intent(getParent(), GalleryPic_Second.class);
                TabGroupActivity parent = (TabGroupActivity) getParent();
                parent.startChildActivity("ArrowsActivity", in);
                startActivityForResult(i, TAKE_PICTURE);
            }
        });


    }




    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) 
            {
                getContentResolver().notifyChange(mUri, null);
                ContentResolver cr = getContentResolver();

                Intent previewMessage = new Intent(getParent(), GalleryPic_Second.class);

                TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                parentActivity.startChildActivity("ArrowsActivity", previewMessage);

                try 
                {
                    mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
                    Intent in = new Intent(getParent(), GalleryPic_Second.class);
                    GalleryPic_Second.bmp = mPhoto;
                    TabGroupActivity parent = (TabGroupActivity) getParent();
                    parentActivity.startChildActivity("ArrowsActivity", in);

                }

                catch (Exception e) 
                {
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }


            }


            break;
        case REQUEST_ID :
            InputStream stream = null;
             if (resultCode == Activity.RESULT_OK) {

                 try
                 {
                    stream = getContentResolver().openInputStream(data.getData());
                    Bitmap original = BitmapFactory.decodeStream(stream);
                    Intent in = new Intent(getParent(), GalleryPic_Second.class);
                    GalleryPic_Second.bmp = Bitmap.createScaledBitmap(original, original.getWidth()/HALF, original.getHeight()/HALF, true);
                    TabGroupActivity parent = (TabGroupActivity) getParent();
                    parent.startChildActivity("ArrowsActivity", in);                           
                 }

                 catch (Exception e) 
                 {
                    e.printStackTrace();
                 }
                if (stream != null) {
                    try {
                        stream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }



             }


        }
    }


    @Override
    public void onBackPressed() {
    // this.getParent().onBackPressed();
         TabViewLayout parentTab = (TabViewLayout) this.getParent();
         parentTab.switchTabBar(1);

    }

  } 

我在这里面临的是,我没有在儿童活动组中选择图像。我已经搜索了它并找到了这个解决方案... OnActivityResult is not working in TabActivityGroup?但是我没有得到这个链接中给出的解释。需要做什么?

1 个答案:

答案 0 :(得分:3)

嗨,将您的ActivityResult代码放在父级中的GroupActivity中,因为它将值返回给它的父级而不是子级。

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
}
}

上面的代码放在Activity组中。我希望它对你有用,因为它对我很有好处: - )。