【问题标题】:android open dialogue activity without opening main activity behind itandroid打开对话活动而不打开它后面的主要活动
【发布时间】:2010-07-19 14:57:13
【问题描述】:

我正在编写一个程序,在收到短信后提供快速回复对话框。

但是,我得到了意想不到的结果。当我收到一条短信时,会出现相应的对话活动,显示正确的电话号码和消息,但是它后面还有第二个活动,它是我的程序中的“默认”活动(它是我启动应用程序时打开的活动)

我不希望出现第二个活动。快速回复活动应自动出现在用户之前所做的任何事情之上。

“浮动”活动:

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

}

}

在 onReceive() 中调用 Activity

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

清单:

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

【问题讨论】:

    标签: android dialog android-activity


    【解决方案1】:

    在清单中的活动定义中,我发现唯一可行的方法:

    android:launchMode="singleInstance"
    

    但是一旦对话框关闭,您必须重新启动您的主要/默认活动。注意:您将丢失上次启动的所有状态,因此这是一个不太理想的解决方案。

    更新:

    您也可以这样做:

    Intent.FLAG_ACTIVITY_CLEAR_TASK
    

    这就是我所做的:

    1. 打开原始/主要活动
    2. 从服务中,使用上述方法启动对话式活动(主要是再见)。
    3. 当用户关闭对话框时,使用在 onCreate() 中处理的额外意图 (IS_BACK) 再次启动 main 并调用:

      moveTaskToBack(true);

    这将使任务保持在顶部的对话框下,而您的主任务则在堆栈的后面。

    【讨论】:

      【解决方案2】:

      您应该将活动的任务亲和性设置为与您的主要活动不同的东西。这会将其与主要活动分开,并将作为单独的任务进行跟踪:

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

      【讨论】:

        猜你喜欢
        • 2019-02-03
        • 2011-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多