0

I am trying to show a dialog when picture taken so I setup a broadcast receiver on picture taken by default camera app. I read that I need to call an activity to achieve that. I setup the activity so the theme in the manifest is like transparent

android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen

here is my code

public class CameraEventReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

   Cursor cursor = context.getContentResolver().query(intent.getData(),      null,null, null, null);
   cursor.moveToFirst();
   String image_path = cursor.getString(cursor.getColumnIndex("_data"));


   Toast.makeText(context, "New Photo is Saved as : -" + image_path, Toast.LENGTH_SHORT).show();
   Intent i = new Intent(context, DialogActivity.class); 
   i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
   context.startActivity(i);

 }


}

The problem is that my main app activity is being displayed behind the Dialog activity so that the main activity is shown. I just want the dialog activity to be displayed on top of the picture. Is there a way to fix that?

Thank you

EDIT: As requested, my manifest:

   <application
        android:allowBackup="true"
        android:name="MyApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.xx.xx.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DialogAcitivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.xx.xx.DIALOGACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
         </activity>
        <receiver
        android:name=".CameraEventReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="com.android.camera.NEW_PICTURE" />
            <action android:name="android.hardware.action.NEW_PICTURE" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </receiver>

    </application>

My code for button, basically ask the user for a value:

protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();


        promptUser();
    }

    private void promptUser() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);
        builder.setMessage(R.string.msg)
.setCancelable(false)
.setView(input)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = input.getText().toString();

                storeValue(value);

                 dialog.dismiss(); 



        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                dialog.dismiss();
            }
        });
        AlertDialog alert = builder.create();

           alert.show();

    }
Snake
  • 14,228
  • 27
  • 117
  • 250
  • 1
    What is your dialog doing? Can you show that code also? – Anderman Dec 03 '13 at 21:22
  • Can you show your manifest? – chinglun Dec 03 '13 at 21:24
  • I added the code as requested. The dialog just obtain value from user and then I process it – Snake Dec 03 '13 at 21:31
  • 1
    Is your broadcast receiver code in the main activity? I imagine the layout that shows your image is being hidden or removed when the dialog activity is called. Do you mind showing that code that displays the image? – Anderman Dec 03 '13 at 21:37
  • No my broad cast receiver is in its own class (not in an activity). I don't show the image at all. You know when you use your camera app and it takes a pic. It shows the image you just took by default. I want the dialog to go on top of it – Snake Dec 03 '13 at 21:53

2 Answers2

2

the reason your app's activity shows behind the dialog is because android's switching the entire activity task. your dialog activity was (i'm assuming) started in the same task as your other activities.

what you want is to give your dialog activity a different task affinity than the (default) affinity of your other activities.

<activity
    ...
    android:taskAffinity="com.foo.bar.myaffinity"/>

now this activity won't be launched into the same task as the rest of your activities, and when you start it, those other activities won't show behind it.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • Oh WOW! THANK you that is correct. But why is not the i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) creating a new task for it? I thought this is what the flag is for! – Snake Dec 03 '13 at 22:50
  • i think i've exhausted my knowledge, but maybe this: `When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.` – Jeffrey Blattman Dec 03 '13 at 23:06
  • @Jeffry Blattman, I am using transparent background and I tried the the transparent theme, but it keeps showing black background! Any idea why it is not showing the picture it was taken behind? – Snake Dec 17 '13 at 04:54
1

Check this post

What you want is to get your image from the result, then display it in a layout in some activity. Once the image is displayed, launch your dialog.

Community
  • 1
  • 1
Anderman
  • 134
  • 3
  • Thank you, but is a reason why the above behaviour is happeneing? Why is it not displaying the dialog in a transparent activity? Why is main app opening – Snake Dec 03 '13 at 22:14
  • Yes the dialog is showing but not as "one transparent activity only". It is bringing my main app activity as well along with it so it becomes 2 activities (main and dialog activity) on top of the picture – Snake Dec 03 '13 at 22:44