【问题标题】:Android Intent flagsAndroid 意图标志
【发布时间】:2013-07-26 22:19:24
【问题描述】:

我的问题在于理解如何正确使用意图。在谷歌搜索并阅读了关于这个主题的所有文档和文章之后,我仍然无法解决我的问题。我有两个活动:“可搜索”和“ActivityWordInfo”。 “可搜索”活动在数据库中搜索一个词,并显示搜索结果或建议。用户点击其中一个搜索结果后,“ActivityWordInfo”活动将启动并显示单词定义。以下是部分代码:

可搜索:

     @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 活动,它应该已经保存了它的实例状态(结果列表应该仍然存在)。现实中会发生什么: - 用户输入的词被分配给“查询”变量,然后结果和建议显示在“可搜索”中 - 用户单击其中一个单词并创建“ActivityWordInfo” - 然后,当用户向上导航时,会为“可搜索”活动调用 onSaveInstanceState,然后将其删除并创建。结果是一个空布局:(

我不明白为什么“可搜索”被销毁然后创建!这仅在 Android 4.2 中发生,在较低的 API 中不会发生(在 2.3.3 中,正如我预期的那样完美运行)。 JellyBean 中的 Activity 生命周期有什么不同吗?

注意:我不能在清单中使用 parentActivity 属性,因为 ActivityWordInfo 被多个父母调用。

【问题讨论】:

    标签: android android-intent android-activity flags


    【解决方案1】:

    “向上”导航的行为在 2.3 和 4.0 之间发生了变化。我建议你在 developer.android.com 上查看主题“Tasks and Back Stack”。

    【讨论】:

    • 问题是为什么“可搜索”被破坏并且保存的实例丢失了。我认为问题出在我使用的标志上,但我仍然不明白为什么。我使用 FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_SINGLE_TOP 来完成 ActivityWordInfo 并阻止 Searchable 创建新实例。如果我是对的,我现在不知道,但这似乎合乎逻辑。
    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 2013-06-18
    相关资源
    最近更新 更多