1

I have two Activities in which I want to call the second one from the first one's Context Menu. This is what it should do.

  • Activity A Context Menu click should start Activity B.

  • In the onCreate of Activity B, depending on the extras passed in, automatically display a AlertBuilder dialog and then either take a picture or select an image.

What is happening is that when Activity A's Context Menu item is clicked, it launches Activity B and the AlertDialog displays. If I select the option to take a picture, the MediaStore.ACTION_IMAGE_CAPTURE intent is started and once the picture is taken, Activity B is re-launched again and the AlertDialog shows.

Activity A - Context Menu

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        final ItemClass selItem = (ItemClass)this.getListView().getItemAtPosition(info.position);    
        Intent intent;
        SyncData sync;

        switch (item.getItemId()) {
            case R.id.start_activity_b:
            Intent intent = new Intent(ActivityA.this, ActivityB.class);
            intent.putExtra("data1", selItem.itemID);
            intent.putExtra("data2", "AUTO");
            Measurements.this.startActivityForResult(intent, REQUESTCODE_ACTIVITYB);


            return true;
                default:
                    return super.onContextItemSelected(item);
        }
    }

Activity B - onCreate Code

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.listview_main);

        Bundle extras = getIntent().getExtras();
        if ((extras != null) && (extras.containsKey("data1"))) {
            this.itemID = extras.getString("data1");
        }
        if  ((extras != null) && (extra.containsKey("data2"))) {
            this.createAlertDialog();
        }
    } 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
John
  • 35
  • 4

2 Answers2

1

As I stated in my comment, you shouldn't need runOnUiThread() since you are already on the UI Thread. There are a couple ways that may be better than doing this. Again, as in my comment, you should be able to call setResult and finish ActivityB when the Dialog dismisses since you are using startActivityForResult() to start ActivityB.

With just what I see that should be fine. But if it causes problems due to whatever you have in your Dialog or from something else, you can make your Dialog an Activity and give it aDialog themeby adjusting yourmanifest`. Use

<activity android:name=".ActivityName"           
    android:theme="@android:style/Theme.Dialog"> // add this line to give it the effect of a dialog
</activity>

You can start this Activity for result using startActivityForResult(). Then when that result returns to ActivityB you can finish and return to ActivityA or whatever you need to do.

Hope this helped.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

I found the solution to it, after trying over and over to understand why. All that needs to be done is to create a runnable in the Context menu, as it seems to need to be returned quickly otherwise it recreates the Activity. I could be wrong and please feel free to correct me on this one.

Activity A - Context Menu

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        final ItemClass selItem = (ItemClass)this.getListView().getItemAtPosition(info.position);    
        Intent intent;
        SyncData sync;

        switch (item.getItemId()) {
            case R.id.start_activity_b:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(ActivityA.this, ActivityB.class);
                        intent.putExtra("data1", selItem.itemID);
                        intent.putExtra("data2", "AUTO");
                        Measurements.this.startActivityForResult(intent, REQUESTCODE_ACTIVITYB);
                    }
                });

                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
John
  • 35
  • 4
  • 1
    You shouldn't need a `runnable` here. Since you are using `startActivityForResult()` you should be able to call `setResult` and finish `ActivityB` when the `Dialog` dismisses – codeMagic Jun 04 '13 at 14:10