Android Use startActivityForResult from a nested a

2019-05-24 01:20发布

I am writing an application that is composed of several tabs created in a tabhost with:

intent = new Intent().setClass(this, Home.class);
    spec = tabHost.newTabSpec("Home").setIndicator("Home",
                      res.getDrawable(R.drawable.home))
                  .setContent(intent);
    tabHost.addTab(spec);

In the tab in question I am using an ActivityGroup to change to different Activities in the tab:

Intent intent = new Intent(Info1.this, Enroll2.class);
            intent.putExtra("info", Info);

            View newView = Group.group.getLocalActivityManager().startActivity("Info1", intent
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
                    .getDecorView();  
            Group.group.replaceView(newView);

From one of these Activities I need to take a picture and I am trying to use the default camera app on the device using:

//define the file-name to save photo taken by Camera activity
    String fileName = "picture" + Integer.toString(pictureCount);

    //create parameters for Intent with filename
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");

    //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
    imageUri = getContentResolver().insert(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

    //create new Intent
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

This starts the camera app correctly, but after taking a picture it does not enter the onActivityResult method. I've tried placing this method in every class in the chain for the tab and it doesn't enter the code in any of them.

I noticed that this question was asked before at How to startactivityforresult in tab child of TabHost but the only potentially useful answer was a redirect to How to return a result (startActivityForResult) from a TabHost Activity? which is a question about using startActivityForResult from a basic Activity to start a tabActivity and not starting an Activity from a tabActivity so it was no use.

I also keep seeing people say that this doesn't work when you're using an ActivityGroup, but no one suggests how else to go about doing it.

Any help would be appreciated.

1条回答
Melony?
2楼-- · 2019-05-24 01:47

Alright, I was able to find a solution to this problem.

First, I created another activity that I started using the basic startActivity() call I called a Result Controller. This does not pass any data back to the tabbed activity which means you don't have to worry about where it's going.

Second, I created a simple static data class which I called DataConnector. The ResultController would get the instance of the DataConnector and insert the Data there

Then, in the original activity(in the tabs) I implemented the onWindowFocusChanged method to determine when the user is back to it. I got the instance of DataConnector and was able to pull the data I needed out of there.

查看更多
登录 后发表回答