我和OP有同樣的問題,但有一個解決方案,並不意味着放棄使用SearchView
。這不是完美的解決方案,但它相當乾淨,似乎運作良好。
正如OP所述......
,當我嘗試推出從它自己的類活動的新實例,Android的不尊重FLAG_ACTIVITY_NEW_TASK並沒有推新的任務壓入堆棧
...所以我解決方案是通過中介活動去。爲此,我創建了一個DummyActivity
類,該類基本上採用原始意圖並將其轉發到目標活動。所以從我原來的活動,而不是調用...
Intent destinationIntent = new Intent(getActivity(), DestinationActivity.class);
startActivity(destinationIntent);
...我叫......
/*
* Create the intent as normal, but replace our destination Activity with DummyActivity
*/
Intent dummyIntent = new Intent(getActivity(), DummyActivity.class);
/*
* This will make it so that if user clicks back from the next activity, they won't be shown DummyActivity
*/
dummyIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
/*
* Tell DummyActivity about the destination Activity
*/
dummyIntent.putExtra(DummyActivity.EXTRA_DESTINATION_ACTIVITY, YourDestinationActivity.class); //IMPORTANT - the destination Activity must implement Serializable
/*
* If you want, you can add any other extras to dummyIntent (which will be passed through to the destination activity). Launch DummyActivity.
*/
startActivity(dummyIntent);
在我的清單
,DesinationActivity被定義爲singleTop。
而且如果你需要的唯一的另一件事是不應該需要任何重大的定製DummyActivity類...
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class DummyActivity extends Activity {
public static final String EXTRA_DESTINATION_ACTIVITY = "uk.co.mobilewebexpert.EXTRA_DESTINATION_ACTIVITY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_dummy);
Intent intent = getIntent();
/*
* Get destination activity info
*/
@SuppressWarnings("unchecked")
Class<Activity> destinationActivity = (Class<Activity>) intent.getSerializableExtra(EXTRA_DESTINATION_ACTIVITY);
Bundle destinationActivityExtras = intent.getExtras();
/*
* Launch destination activity
*/
Intent destinationActivityIntent = new Intent(this, destinationActivity);
destinationActivityIntent.putExtras(destinationActivityExtras);
destinationActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(destinationActivityIntent);
}
/**
* Override super.onNewIntent() so that calls to getIntent() will return the latest
* intent, rather than the first intent.
*/
@Override
public void onNewIntent(Intent intent){
super.onNewIntent(intent);
setIntent(intent);
}
}
希望這會爲你工作了。建議歡迎。乾杯。
從完全沒有答覆(和少量的意見),我會猜測這是一個不平凡的問題。還有一種可能的解決方法(除了避免使用SearchView):我可以設計singleTop Activity來維護自己的狀態堆棧和開關狀態,然後攔截Back按鈕以模擬在任務堆棧中移動。不過,我強烈反感用模擬取代核心操作系統的功能 - 如果這是唯一的選擇,我只會決定谷歌建議SearchView提前用於生產使用,並將創建我自己的搜索操作組件。 – Canuck 2013-02-11 13:46:29