android open dialogue activity without opening main activity behind it

青春壹個敷衍的年華 提交于 2020-01-02 03:36:06

问题


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>


回答1:


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.




回答2:


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"
          >


来源:https://stackoverflow.com/questions/3282204/android-open-dialogue-activity-without-opening-main-activity-behind-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!