开发者

android open dialogue activity without opening main activity behind it

开发者 https://www.devze.com 2023-01-07 17:58 出处:网络
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 t

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());
    }      

}
开发者_JAVA百科

}

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>


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.


You should set the task affinity of the activity to something different than your main activity. This will separate it from the main activity and it will track as a separate task:

<activity android:name=".quickReply"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.Dialog"
          android:launchMode="singleTask"
          android:taskAffinity="quickReply"
          >
0

精彩评论

暂无评论...
验证码 换一张
取 消