簡體   English   中英

android打開對話活動而不打開它背后的主要活動

[英]android open dialogue activity without opening main activity behind it

我正在編寫一個程序,在收到短信時提供快速回復對話框。

但是,我得到了意想不到的結果。 當我收到一條短信時,會出現相應的對話框活動,顯示正確的電話號碼和消息,但是它背后有第二個活動是我程序中的“默認”活動(這是我啟動應用程序時打開的活動)

我不希望第二項活動出現。 快速回復活動應該在用戶以前做過的任何事情之上自己出現。

'浮動'活動:

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()內部活動的調用

        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:launchMode="singleInstance"

但是一旦解除對話框,你必須重新啟動你的主/默認活動。 注意:您將失去上一次發布的所有狀態,因此這不是一個理想的解決方案。

更新:

你也可以這樣做:

Intent.FLAG_ACTIVITY_CLEAR_TASK

所以這就是我做的:

  1. 打開原始/主要活動
  2. 從服務中,使用上面的方法啟動對話框樣式活動(主要是再見)。
  3. 當用戶解除對話框時,再次使用在onCreate()中處理的額外意圖(IS_BACK)啟動main並調用:

    moveTaskToBack(真);

這將使對話框保持在頂部,而主要保留在堆棧的后面。

您應該將活動的任務親和力設置為與主活動不同的內容。 這會將它與主要活動分開,它將作為單獨的任務進行跟蹤:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM