1

My problem is in understanding how to correctly use intents. After googling and reading all the documentations and articles on this topic, I still cannot sort out my problem. I have two activities: "Searchable" and "ActivityWordInfo". The "Searchable" activity searches a word in the database, and displays search results or suggestions. After the user cliks one of the search results, "ActivityWordInfo" activity is launched and diplays the word definition. Here is some part of the code:

Searchable:

     @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;
}

When the user is in ActivityWordInfo and navigates up, I expect to go to Searchable activity, which should have saved its instance state (the list of results should be still there). What in reality happens: -The word typed by the user is assigned to 'query' variable and then the results and suggestions are displayed in "Searchable" -the user clicks one of the words and "ActivityWordInfo" is created -then, when the user navigates up, the onSaveInstanceState is called for the "Searchable" activity, then it is destryed and created. The result is an empty layout :(

I cannot understand why "Searchable" is destroyed and then created! This only happens in Android 4.2 and not in lower APIs (in 2.3.3 worked perfectly as I expected). Is there any difference in the activity lifecycle in JellyBean?

Note: I cannot use the parentActivity attribute in the manifest since the ActivityWordInfo is called by multiple parents.

4

1 回答 1

0

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

于 2013-07-26T22:34:46.477 回答