Android意图标志
我的问题是理解如何正确使用意图。在谷歌搜索和阅读了所有关于这个话题的文献和文章之后,我仍然无法解决我的问题。我有两个活动:“可搜索”和“ActivityWordInfo”。“可搜索”活动在数据库中搜索一个单词,并显示搜索结果或建议。用户点击其中一个搜索结果后,启动“ActivityWordInfo”活动并显示单词定义。以下是部分代码: 可搜索:Android意图标志,android,android-intent,android-activity,flags,Android,Android Intent,Android Activity,Flags,我的问题是理解如何正确使用意图。在谷歌搜索和阅读了所有关于这个话题的文献和文章之后,我仍然无法解决我的问题。我有两个活动:“可搜索”和“ActivityWordInfo”。“可搜索”活动在数据库中搜索一个单词,并显示搜索结果或建议。用户点击其中一个搜索结果后,启动“ActivityWordInfo”活动并显示单词定义。以下是部分代码: 可搜索: @Override public void onCreate(Bundle savedInstanceState) {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Get the intent, verify the action and get the query
if( savedInstanceState != null ){
//the application is being reloaded
query = savedInstanceState.getString("searchedWord");
doMySearch(query); //does the search in the database
}else{
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("searchedWord", query);
//saves the searched word if this activity is killed
}
@Override
public void onClick(View v) { //when one of the search results is clicked
int wordID = (Integer) v.getTag();
Intent intent = new Intent(Searchable.this, ActivityWordInfo.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle b = new Bundle();
b.putInt("key", wordID);
b.putInt("calling_activity", callingActivityId);
intent.putExtras(b);
startActivity(intent);
}
ActivityWordInfo:
public void onCreate(Bundle savedInstanceState) {
...
Bundle b = getIntent().getExtras();
current_word_id = b.getInt("key", 0);
callingActivityId = b.getInt("calling_activity", 0);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
switch(callingActivityId){
case 3: //which is the ID of Searchable activity
Intent intent3 = new Intent(ActivityWordInfo.this, Searchable.class);
intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent3);
break;
}
break;
}
当用户在ActivityWordInfo中并向上导航时,我希望转到Searchable activity,它应该已经保存了它的实例状态(结果列表应该仍然存在)。实际情况是:
-用户键入的单词被分配给“query”变量,然后结果和建议显示在“Searchable”中
-用户单击其中一个单词,并创建“ActivityWordInfo”
-然后,当用户向上导航时,将为“可搜索”活动调用onSaveInstanceState,然后对其进行销毁和创建。结果是一个空布局:(
我无法理解为什么“Searchable”会被销毁然后被创建!这只发生在Android 4.2中,而不是在较低的API中(2.3.3中的工作如我所预期的那样完美)。JellyBean中的活动生命周期有什么不同吗
注意:我无法在清单中使用parentActivity属性,因为ActivityWordInfo由多个父项调用。在2.3和4.0之间更改了“向上”导航的行为。我建议您查看developer.android.com上的主题“”。问题是为什么“可搜索”正在被销毁,保存的实例已丢失。我认为问题在于我使用的标志,但我仍然无法理解原因。我使用了flag\u ACTIVITY\u CLEAR\u TOP flag\u ACTIVITY\u SINGLE\u TOP来完成ActivityWordInfo并阻止Searchable创建新实例。我现在不知道我是否正确,但这似乎合乎逻辑。