[英]intent and onNewIntent
我有一個應用程序,具有一個名為NfcScannerActivity的主菜單屏幕。 目前,清單中沒有啟動模式(標准)。 如果單擊getRota,將帶您到清單屏幕中定義為“ singleTask”的旋轉屏幕,它只是Web調用中數據的列表視圖。
在旋轉屏幕中,您可以從選項菜單欄中單擊nextRota。 發生這種情況時,將啟動一個指定菜單屏幕(NfcScannerActivity)的意圖,因為這是進行網絡呼叫以獲取第二天的輪轉數據的位置。 檢索到數據后,將再次啟動旋轉屏幕。
所有這些工作正常,但我確信由於任務中有多個菜單屏幕實例,因此應用程序中存在一些問題。 如果我將NfcScannerActivity指定為'SingleTask',則當您單擊下一個Rota時,它將停留在菜單屏幕上,就好像它沒有處理“ NEXT_ROTA”意向操作一樣。
我了解我可能必須重寫NfcScannerActivity活動中的onNewIntent。
怎么做? 我已經嘗試了以下方法。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
。
這似乎無法處理“ NEXT_ROTA”意圖動作。 謝謝馬特。
[edit1]
這是當用戶從選項菜單中單擊next_rota時我在rota活動中所擁有的。
Intent i = new Intent(this, NfcscannerActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("nextRota", nextDay);
i.setAction("NEXT_ROTA");
startActivity(i);
。
然后在NfcScannerActivity的onCreate中,我有以下內容。
if(intent.getAction().equalsIgnoreCase("NEXT_ROTA")){
Log.e(TAG, "next rota action");
String date = intent.getStringExtra("nextRota");
getNextRota(date);
}
。
getNextRota(date)調用AsyncTask進行網絡調用以獲取下一個旋轉數據。 在onPostExecute中,它執行以下操作。
Intent intent = new Intent(NfcscannerActivity.this,
GetRota.class);
Bundle b = new Bundle();
b.putSerializable("rotaArray", rotaArray);
intent.putExtra("rotaArrayBundle", b);
startActivity(intent);
。
所以我已經在onCreate的NfcScannerActivity中處理了'NEXT_ROTA'意向動作。 我必須在onNewIntent中做同樣的事情嗎?
嘗試以下
在輪轉屏幕活動中,當“啟動意圖以指定菜單屏幕”時
Intent intent = new Intent(<rota screen activity>, NfcScannerActivity.class);
intent.setAction("NEXT_ROTA");
//this brings the previous existing activity to the front of the stack
//instead of creating a new one
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
在NfcScannerActivity中
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent.getAction().equals( "NEXT_ROTA")){
String date = intent.getStringExtra("nextRota");
getNextRota(date);
}
}
上面應該做的就是允許您創建任意數量的Rota屏幕,但只能擁有一個NfcScannerActivity
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.