Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android open dialogue activity without opening main activity behind it

Im writing a program that offers a quick reply dialog upon receipt of an SMS.

However, I am getting an unexpected result. When I receieve an SMS, the appropriate dialog activity comes up displaying the correct phone number and message, however there is a second activity behind it that is the 'default' activity in my program (it is what opens when i launch my application)

I do not want this second activity to come up. The quick reply activity should come up by itself over top of whatever the user was doing before.

The 'floating' activity:

public class quickReply extends Activity {
String mNumber, mMessage;
TextView mMainText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mMainText = (TextView)findViewById(R.id.mainText);

    try{
        Intent i = getIntent();
        Bundle extras = i.getExtras();

        mNumber = extras.getString("theNumber");
        mMessage = extras.getString("theMessage");
         this.setTitle("Message From:" + mNumber);
         mMainText.setText(mMessage);


    } catch(Exception e) {
        mMainText.setText(e.getMessage());
    }      

}

}

The call to the activity inside an onReceive()

        Intent i = new Intent(context, quickReply.class);
    i.putExtra("theNumber", mNumber);
    i.putExtra("theMessage", mMessage);
    i.setFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);

The Manifest:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".quickReply"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.Dialog"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
       <receiver android:name=".SmsReceiver"> 
        <intent-filter> 
            <action android:name=
                "android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter> 
    </receiver>

</application>

like image 437
jwheels Avatar asked Jul 19 '10 14:07

jwheels


1 Answers

the only way I have found that works, in your activity definition in manifest:

android:launchMode="singleInstance"

but then you have to relaunch your main/default activity once the dialog is dismissed. NOTE: you will lose all state from the previous launch, so this is a less than ideal solution.

UPDATE:

you can also do this by:

Intent.FLAG_ACTIVITY_CLEAR_TASK

so here's what I did:

  1. open the original/main activity
  2. from a service, launch the dialog style activity using the above (main goes bye-bye).
  3. when the user dismisses the dialog, start main again with an extra intent (IS_BACK) that is processed in onCreate() and calls:

    moveTaskToBack(true);

this will keep the task under the dialog on top and your main in the back of the stack.

like image 119
droideckar Avatar answered Nov 18 '22 13:11

droideckar